Path comparison in 1.URL path and routing configuration
2. From the above figure, we can draw the following set of routing data
3. Two of Righteousness
If we also create a new home controller in models, then you will notice the error after re-flushing. And this is because there is no way to determine exactly which controller to call the reason for the request, of course, you think as long as we do not create the same name of the controller, so you can only control your project does not appear, but you do not have control of your loaded library will not appear, but the ASP. MVC has provided a solution to us, as corrected below
RouteConfig.cs:
public class Routeconfig {public static void RegisterRoutes (RouteCollection routes) { routes. MapRoute ( name: "Default", URL: "{controller}/{action}/{id}", defaults:new {controller = "Home", action = "Index", id = urlparameter.optional}, namespaces:new[] {"Mvcstudy.controllers"} ); } }
We can see that the namespaces parameter, by passing the name of the namespace into it, can be a question of excluding ambiguity.
4. Constrained routing
Above we have a {ID} to capture the parameters, but you also found that it can catch any string and so on, but we sometimes need to limit it, such as let it only enter numbers, then we can use the regular expression to constrain it.
public class Routeconfig {public static void RegisterRoutes (RouteCollection routes) { routes. MapRoute ( name: "Default", URL: "{controller}/{action}/{id}", defaults:new {controller = "Home", action = "Index", id = urlparameter.optional}, constraints:new{id = @ "^\d*$"}, namespaces:new[] {" Mvcstudy.controllers "} ); } }
Routing chapter of ASP. NET MVC Learning