"Basic" MVC routing rules

Source: Internet
Author: User

Tag: Pat Reflection param Register requires additional data option admin src

First, Routedata parsing process

in ASP. NET MVC, after the server receives a request from the client, it passes through some column processing to get the requested data, such as in the pipeline pipeline event, by subscribing to the appropriate event, HttpContext as a parameter into the httpcontextwrapper to encapsulate, and then get the current route collection of data Routedata to parse, get specific parameters, including the request path, request parameters, Iroutehandler, etc. Returns a IHttpHandler object through Iroutehandler's Gethttphandler, which processes the request through the object, The controller factory then constructs a Excute method that Controller,controller calls IController by reflection from the matching controller in the Routedata, which is also the action that gets the current request by reflection. Finally, the action is executed, the client data is returned, and the request is completed. The overall flowchart is as follows:

In this process, the routes in routedata play a significant role. Routing function: first through the HTTP request, and resolve the URL request in the Controller and action as well as additional data, and then pass the identified data to the Controller's action (Controller's method). This is the two important functions of the routing component!

Second, the route rule instance resolves

Example one: First of all, the system provides the default routing format, the following is the system to the default code:

1  Public Static voidregisterroutes (routecollection routes)2 {3Routes. Ignoreroute ("{Resource}.axd/{*pathinfo}");4 5 routes. MapRoute (6         "Default",//Route name7         "{Controller}/{action}/{id}",//URL with Parameters8         New{controller ="Home", action ="Index", id = urlparameter.optional}//parameter Default value9     );Ten}

The URL format is: http://localhost:80/home/index corresponding rule is:{controller}/{action}/{id} The blackbody part is the corresponding part. This is still the case with a default value.

The detailed match should be: http://localhost:80/custom/detials/1 . Remove the host domain name, the remaining correspondence is matching controller and Actiong. Parsing this url,controller through the routing component is custom,action is detials. The pass-through ID is 1. This is the use of Maproute (string name, string URL, object defaults), and the overloading of this method.

Example two: URL routing rules that do not use default values

Function Signature:MapRoute (string name, string URL);

Routes. MapRoute ("No default routing Rule", "{controller}/{id}-{action}");

Example of a suitable URL: http://localhost:80/custom/1-detials

it will not match http://localhost:80/

Instance three: URL routing rules with namespaces

Function Signature:MapRoute (string name, string URL, string[] namespaces);//route name, URL rule, namespace

1 routes. MapRoute (2             "Myurl",//Route name3             "{controller}/{id}-{action}",//URL with Parameters4             New{controller ="Home", action ="Index", id = urlparameter.optional},//parameter Default value5             New string[] {"mvcdemo.controllers"}//name Space6);

Url:http://localhost:0000/custom/1-detials

This example is a routing rule with a namespace, which is useful when Aeras is used. Not much, say it back!

Example four: Routing Rules with constraints

function Signature: MapRoute (string name, string URL, object defaults, object constraints);//route name, URL rule, default value, namespace

1 routes. MapRoute (2              "Rule1",3              "{Controller}/{action}-{year}-{month}-{day}}",4              New{controller ="Home", action ="Index", year =" .", Month ="Geneva", day =" +" },5              New{year =@"^\d{4}", Month =@"\d{2}" }6);

Example five: A routing rule with a namespace, with constraints, with default values

function Signature: MapRoute (string name, string URL, object defaults, object constraints, string[] namespaces);

1 routes. MapRoute (2                  "Rule1",3                  "Admin/{controller}/{action}-{year}-{month}-{day}",4                  New{controller ="Home", action ="Index", year =" .", Month ="Geneva", day =" +" },5                  New{year =@"^\d{4}", Month =@"\d{2}" },6                  New string[] {"mvcdemo.controllers" }7);

url:http://localhost:14039/Admin/home/index-2010-01-21

Example six: Capturing all the routes

1 routes. MapRoute (2                  " All",//Route name3                 "{*vauler}",//URL with Parameters4                  New{controller ="Home", action ="Index", id = urlparameter.optional}//parameter Default value5);
 1  routes. Ignoreroute ( {resource}.axd/{*pathinfo}  ); //  
1 arearegistration.registerallareas (); // register all the areas 2 registerroutes (routetable.routes); // registering a custom rule 3 4 /// /debug With statement, need to download RouteDebug.dll, and add Reference! After adding this sentence, you can test the URL route.  5//RouteDebug.RouteDebugger.RewriteRoutesForTesting (routetable.routes);

"Basic" MVC routing rules

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.