MVC Routing Uses

Source: Internet
Author: User
Tags custom name

URL attribute routing: attribute routing can customize the routing rules in the Controller and action. The disadvantage is that it is not a good unified management URL

To register an attribute route:

1  Public Static voidregisterroutes (routecollection routes)2         {3Routes. Ignoreroute ("{Resource}.axd/{*pathinfo}");4 5Routes. Mapmvcattributeroutes ();//registering feature Routes6 7 routes. MapRoute (8Name"Default",9Url:"{Controller}/{action}/{id}",TenDefaultsNew{controller ="Home", action ="Index", id =Urlparameter.optional} One             ); A         } -     //Use the routing attribute on the controller to omit the controller inside the action -[Routeprefix ("Home")]   the      Public classHomecontroller:controller -     { -          Publicactionresult Index () -         { +             returnView (); -         } +         //Attribute Routing A         //URLhttp://localhost: 17749/HOME/ORDER/2 at         //page data Format validation -[Route ("Home/order/{page:int}")] -          PublicActionResult OrderList (intpage) -         { -Viewbag.pageindex =page; -             returnView (); in}

2. Traditional route traditional routes can be centrally configured routing rules, procedures for later maintenance is more convenient

1          Public Static voidregisterroutes (routecollection routes)2         {3Routes. Ignoreroute ("{Resource}.axd/{*pathinfo}");4 5Routes. Mapmvcattributeroutes ();//registering feature Routes6             //the custom name must precede the default route7 routes. MapRoute (8                 "Order",9                 "Order/{id}",Ten                 New{controller ="Order", action ="Details", id =urlparameter.optional}, One                 New{id =@"\d" } A             ); -  - routes. MapRoute ( theName"Default", -Url:"{Controller}/{action}/{id}", -DefaultsNew{controller ="Home", action ="Index", id =Urlparameter.optional} -             ); +  -         } +  A  at  -[Routeprefix ("Order")] -      Public classOrdercontroller:controller -     { -         //Attribute Routing -[Route ("")] in[Route ("list/{page:int?}")] -          PublicActionResult Index (intpage =1) to         { +Viewbag.pageindex =page; -             returnView (); the         } *         //Legacy Routing $          PublicActionResult Details (intID)Panax Notoginseng         { -Viewbag.id =ID; the             returnView (); +         } A}

    

  

<a href= "@Url. ROUTEURL (" Order ", new {id = 1})" target= "_blank" >order Details 1</a>
<a href= "@Url. ROUTEURL (" Order ", new {id = 2})" target= "_blank" >order Details 2</a>

MVC Routing Uses

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.