The most comprehensive ASP. net mvc routing configuration in history (1)
XD first describes the URL construction. In fact, this cannot be constructed, but it is just a syntax feature.
1. Naming parameter specifications + anonymous objects
routes.MapRoute(name: "Default",url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } );
Construct a route and add
- Route myRoute = new Route("{controller}/{action}", new MvcRouteHandler());
- routes.Add("MyRoute", myRoute);
2. Direct Method overload + anonymous object
- routes.MapRoute("ShopSchema", "Shop/{action}", new { controller = "Home" });
I personally think the first one is easier to understand, the second is easier to debug, and the third is more efficient to write. Take all the necessary information. This article is biased towards the third.
1. Default route (built-in MVC)
- Routes. MapRoute (
- "Default", // route name
- "{Controller}/{action}/{id}", // URL with Parameters
- New {controller = "Home", action = "Index", id = UrlParameter. Optional} // default value of the parameter (UrlParameter. Optional-Optional ));
2. Static URL segments
- routes.MapRoute("ShopSchema2", "Shop/OldAction", new { controller = "Home", action = "Index" });
-
- routes.MapRoute("ShopSchema", "Shop/{action}", new { controller = "Home" });
- routes.MapRoute("ShopSchema2", "Shop/OldAction.js",
- new { controller = "Home", action = "Index" });
No placeholder routing is a ready-made write.
For example, write and access http: // localhost: XXX/Shop/OldAction. js. response is completely OK. The three reserved words controller, action, and area should not be set in static variables.
3. Customize regular variable URL segments
- routes.MapRoute("MyRoute2", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = "DefaultId" });
In this case, if you access/Home/Index, because the third (id) has no value, this parameter will be set to DefaultId according to the routing rule.
The assignment of viewbag to the title is obvious.
- ViewBag.Title = RouteData.Values["id"];
The result is that the title is DefaultId. Be sure to assign a value to the Controller. You cannot compile the value in the view.
4. redescribe the default route
Then return to the default route. UrlParameter. Optional is an Optional URL segment. If this parameter is not found in the route, the id is null. According to the original article, this optional URL segment can be used to separate concerns. It is not very good to set the parameter default value directly in the routing just now. According to my understanding, the actual parameters are sent by the user. All we do is define the parameter name. However, if you want to assign a default value to the parameter, we recommend that you use the syntax sugar to write it into the action parameter. For example:
- public ActionResult Index(string id = "abcd"){ViewBag.Title = RouteData.Values["id"];return View();}
5. Variable Length Routing
- routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
Here the id and the last segment are both variable, therefore,/Home/Index/dabdafdaf is equivalent to/Home/Index/abcdefdjldfiaeahfoeiho and is equivalent to/Home/Index/All/Delete/Perm /.....
6. Cross-namespace Routing
Remember to reference the namespace to enable the IIS website or 404. This is very non-mainstream. We do not recommend that you do anything about it.
- routes.MapRoute("MyRoute","{controller}/{action}/{id}/{*catchall}", new { controller = "Home", action = "Index", id = UrlParameter.Optional },new[] { "URLsAndRoutes.AdditionalControllers", "UrlsAndRoutes.Controllers" });
However, in this case, the array is ranked in no particular order. If Multiple matching routes exist, an error is returned. Then the author proposes an improved writing method.
- routes.MapRoute("AddContollerRoute","Home/{action}/{id}/{*catchall}",new { controller = "Home", action = "Index", id = UrlParameter.Optional },new[] { "URLsAndRoutes.AdditionalControllers" });
-
- routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}", new { controller = "Home", action = "Index", id = UrlParameter.Optional },new[] { "URLsAndRoutes.Controllers" });
In this way, if the first URL segment is not Home, it will be handed over to the second for processing. Finally, if this route cannot be found, it will not leave a path for the later route, so it will not be searched down.
- Route myRoute = routes.MapRoute("AddContollerRoute",
- "Home/{action}/{id}/{*catchall}",
- new { controller = "Home", action = "Index", id = UrlParameter.Optional },
- new[] { "URLsAndRoutes.AdditionalControllers" }); myRoute.DataTokens["UseNamespaceFallback"] = false;
7. Regular Expressions match routes
- routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}",
- new { controller = "Home", action = "Index", id = UrlParameter.Optional },
- new { controller = "^H.*"},
- new[] { "URLsAndRoutes.Controllers"});
Restrict multiple URLs
- routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}",
- new { controller = "Home", action = "Index", id = UrlParameter.Optional },
- new { controller = "^H.*", action = "^Index$|^About$"},
- new[] { "URLsAndRoutes.Controllers"});
8. Specify the Request Method
- routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}",
-
- new { controller = "Home", action = "Index", id = UrlParameter.Optional },
-
- new { controller = "^H.*", action = "Index|About", httpMethod = new HttpMethodConstraint("GET") },
-
- new[] { "URLsAndRoutes.Controllers" });