The most comprehensive ASP. net mvc routing configuration in history (1)

Source: Internet
Author: User

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

 
 
  1. Route myRoute = new Route("{controller}/{action}", new MvcRouteHandler()); 
  2. routes.Add("MyRoute", myRoute);  

2. Direct Method overload + anonymous object

 
 
  1. 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)

 
 
  1. Routes. MapRoute (
  2. "Default", // route name
  3. "{Controller}/{action}/{id}", // URL with Parameters
  4. New {controller = "Home", action = "Index", id = UrlParameter. Optional} // default value of the parameter (UrlParameter. Optional-Optional ));

2. Static URL segments

 
 
  1. routes.MapRoute("ShopSchema2", "Shop/OldAction", new { controller = "Home", action = "Index" });  
  2.   
  3. routes.MapRoute("ShopSchema", "Shop/{action}", new { controller = "Home" });  
  4. routes.MapRoute("ShopSchema2", "Shop/OldAction.js", 
  5.  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

 
 
  1. 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.

 
 
  1. 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:

 
 
  1. public ActionResult Index(string id = "abcd"){ViewBag.Title = RouteData.Values["id"];return View();}  

5. Variable Length Routing

 
 
  1. 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.

 
 
  1. 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.

 
 
  1. routes.MapRoute("AddContollerRoute","Home/{action}/{id}/{*catchall}",new { controller = "Home", action = "Index", id = UrlParameter.Optional },new[] { "URLsAndRoutes.AdditionalControllers" });  
  2.   
  3. 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.

 
 
  1. Route myRoute = routes.MapRoute("AddContollerRoute",  
  2. "Home/{action}/{id}/{*catchall}",  
  3. new { controller = "Home", action = "Index", id = UrlParameter.Optional },  
  4. new[] { "URLsAndRoutes.AdditionalControllers" });  myRoute.DataTokens["UseNamespaceFallback"] = false;  

7. Regular Expressions match routes

 
 
  1. routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}", 
  2.  new { controller = "Home", action = "Index", id = UrlParameter.Optional }, 
  3.  new { controller = "^H.*"},  
  4. new[] { "URLsAndRoutes.Controllers"}); 

Restrict multiple URLs

 
 
  1. routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}", 
  2. new { controller = "Home", action = "Index", id = UrlParameter.Optional },  
  3. new { controller = "^H.*", action = "^Index$|^About$"},  
  4. new[] { "URLsAndRoutes.Controllers"});  

8. Specify the Request Method

 
 
  1. routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}", 
  2.   
  3. new { controller = "Home", action = "Index", id = UrlParameter.Optional },  
  4.   
  5. new { controller = "^H.*", action = "Index|About", httpMethod = new HttpMethodConstraint("GET") },  
  6.   
  7. new[] { "URLsAndRoutes.Controllers" });  


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.