How are the default MVC Controllers added to the route table - asp.net-mvc

We says that the MVC request is processed by the controller/action which are mapped in the Routetable.
In routeconfig file, there is only one (default) route is mapped. But how all the controller/action excecuted correctly. How the other routes are added in the routetable ?

The default route works by convention, which looks for classes in the Controllers folder that end with the word controller (i.e. OrdersController). In this case the route will automatically be added to your routing table.
You can find the full documentation for ASP.Net Routing here: https://msdn.microsoft.com/en-us/library/cc668201.aspx

You need to do some reading here to fully understand how ASP.NET routing works.
https://learn.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/controllers-and-routing/asp-net-mvc-routing-overview-cs

Related

How can I make URL routing for subfolders in MVC?

I am new to the Asp.net MVC4 , I need to define rout for folder structure mention below,
Views/Home (Desired URL: /Home)
Views/Admin/CorePages/City (Desired URL: /Admin/CorePages/City)
for 'Home' URL routing working perfect but for sub-folder such as 'Admin/CorePages/City' it is not routing as per URL,How do I create route?
Note: My Controller are place in same manner in controller folder.

Special area route (ASP.NET MVC)

I have a unusual problem with ASP.NET MVC3 Routing. I created a area named "Account" and inside that a controller "Main" with action "Login". Now I wanted to create a route, that would look something like this: "/Login" (that means no "/Account/Main/Login"), but I keep failing to do that (I have used the AccountAreaRegistration to register routes, but #Html.ActionLink always skips them and chooses the default area route ("/Account/{controller}/{action}", thus the URL is different from what I want). How can I proceed and solve this issue?
I have solved my issue, it was actually quite stupid. The order of the routes was OK, but the problem was caused by the fact I did use area = "Account" default value in the list of MapRoute's default values. After I deleted it, everything works like a charm.

Why doesn't Default route work using Html.ActionLink in this case?

I have a rather peculiar issue with routing.
Coming back to routing after not having to worry about configuration for it for a year, I am using the default route and ignore route for resources:
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default",
// Route name
"{controller}/{action}/{id}",
// URL with parameters
new
{
controller = "Home",
action = "Index",
id = UrlParameter.Optional
});
I have a RulesController with an action for Index and Lorem and a Index.aspx, Lorem.aspx in Views > Rules directory.
I have an ActionLink aimed at Rules/Index on the maseter page:
<li><div><%: Html.ActionLink("linkText", "Index", "Rules")%></div></li>
The link is being rendered as http://localhost:12345/Rules/ and am getting a 404.
When I type Index into the URL the application routes it to the action.
When I change the default route action from "Index" to "Lorem", the action link is being rendered as http://localhost:12345/Rules/Index adding the Index as it's no longer on the default route and the application routes to the Index action correctly.
I have used Phil Haack's Routing Debugger, but entering the url http://localhost:12345/Rules/ is causing a 404 using that too.
I think I've covered all of the rookie mistakes, relevant SO questions and basic RTFMs.
I'm assuming that "Rules" isn't any sort of reserved word in routing.
Other than updating the Routes and debuugging them, what can I look at?
Make sure there is not a folder called 'Rules' in the same directory as your website. In its default configuration, ASP.NET MVC routes will respect physical paths before route definitions. If there is a route defined which matches the path to a physical folder in the website, the routing engine will be bypassed completely.
You can disable routing to physical paths by changing the RouteTable.Routes.RouteExistingFiles property to false, but if you do this and your application has paths to physical resources (such as images, scripts, stylesheets, etc) you will need to accommodate for those paths with matching IgnoreRoute() definitions.
For example: RouteTable.Routes.IgnoreRoute("content/{*pathInfo}");.

ASP.NET MVC - Is it possible to generate cross-application action links?

is it possible to generate cross-application action links using the HTML helper class?
I have the following (both are separate VS2008 projects):
http://mainwebsite/
http://mainwebsite/application
I would like to generate a link IN the /mainwebsite/application/ project TO /mainwebsite/. Is this possible? Or should I just hardcode a hyperlink?
addition:
My question also applies vice-versa. So generating a link IN /mainwebsite/ TO /mainwebsite/application/. I already managed to do like so, by simulating the application name as controller name:
<% =Html.ActionLink("ApplicationName","",new With {.Controller = "application" }) %>
Yes, you can do this. The HTML helper generates URLs using the routing system, which is completely ignorant of ASP.NET MVC and your application. If you have a route in your route table for the other application, then you can generate a URL for it using the HTML helper. There is no rule which states that your route table can only contain routes for the current application, although you obviously need to be careful about how you order the routes.

Issue with Default and catchall routes

I define a lot of explicit routes. One of them is:
routes.MapRoute("default", "",
new { controller = "Home", action = "Index" });
At the end, I define a catchall route:
routes.MapRoute("PageNotFound", "{*url}",
new { controller = "Error", action = "Http404" });
If I go to the homepage http://localhost, then the http404 page is shown. And strangely, if I remove the catchall route, then the welcome page appears correctly.
Note also that I have a menu where I call Url.RouteUrl("default") and the link to the homepage is correctly generated.
So, why is my default route not activated when the catchall route exists?
Update: I'm using routes.RouteExistingFiles=true. If I remove it, then it works as expected. But I need it to be set to true. What's the problem here?
Thanks.
If you use "routes.RouteExistingFiles=true" it means it will route existing (physically exist) files as its own - so routing will be skipped for those. I think in your root website there is probably a "default.aspx" or "index.htm" or something like that.
Turning on RouteExistingFiles will then allow those files to be executed normally (instead of via routing).
Now I think what happen is that your catchall routing is overriding you RouteExistingFiles - so it automatically routes the default.aspx into your 404 catchall.
If you still have the default route (I.E. {controller}/{action}/{id}) in RegisterRoutes() it will trap all URLs that match the format of a normal MVC request.
In other words the catch-all route can only intercept a bad URL if it doesn't fit the normal format (blah/blah/blah/blah).
In the case of a non-existent controller the exception must be handled through conventional ASP.NET handling.
Theres a good description of handling this here
Did you try to put a constraint on the catch all route? Constraint should tell it that the catch-all segment should not have 0 characters.

Resources