Custom routing rules are required, with a small range written in front, and a large range written behind.
A routing rule can register multiple bars,
The name of a routing rule cannot be duplicated
Routing rules are ordered and matched in order, suggesting that a small range is written before, and a wide range is written.
Routing rules can set constraints-regular expressions
Controllers that match routing rules can set namespace constraints
Routes. MapRoute (
Name: "NewShow",
URL: "{year}-{month}-{day}-{id}",
defaults:new {controller = "News", action = "Show"},
Constraints:new
{
[Email protected] "^\d{4}$",
month = @ "^\d{1,2}$",
Day = @ "^\d{1,2}$"
}
);
Routes. MapRoute (
1. Parameter name sets the name of the routing rule must be unique, repeating the exception by default the routing rule to add "default"
Name: "Default",
2. Parameter URL set URL routing rules, variable values use {} In the keyword controller, action name is not variable
URL: "{controller}/{action}/{id}",
3. Parameter defaults set the default value type of the parameter in the routing rule to object, you can pass an anonymous object, The Urlparameter.optional property depends on the parameter parameter defined in the rule, which represents an optional read-only parameter if some of the parameters in the routing rule are not specified in the actual URL, the default value is used as the value of the parameter
defaults:new {controller = "Home", action = "Index", id = urlparameter.optional}
);
Several objects in the route:
1. Routing rules route: You can view the source code to understand the construction method, you need to specify the route format, default value, processor three values. The code is as follows:
Routes. MapRoute (
Name: "Default",
URL: "{controller}/{action}/{id}",
defaults:new {controller = "Home", action = "Index", id = urlparameter.optional}
);
2. Routing data routedata: an object that the current request context matches a routing rule and can be passed routedata.getrequeststring ("Controller/action") in action Get the real name of the control or behavior in these requests
3. Route Collection RouteCollection: A collection collection<routebase> that holds routing rules; In an MVC project, you can configure multiple routing rules to be stored in the route collection in the format of a key-value pair
4. routing Table routetable: class contains static RouteCollection property, complete global storage of all routing rules, complete registration in global
The customer's request process is:
When a client requests to come over, HttpContext requests to routecollection to go to the routetable of the first key-value pair if there is one that is more successful than the routing rule, Generates a routedata data and jumps to the corresponding Cotroller action to do the corresponding processing.
Routes. MapRoute (
1. Parameter name sets the name of the routing rule must be unique, repeating the exception by default the routing rule to add "default"
Name: "Default",
2. Parameter URL set URL routing rules, variable values use {} In the keyword controller, action name is not variable
URL: "{controller}/{action}/{id}",
3. Parameter defaults set the default value type of the parameter in the routing rule to object, you can pass an anonymous object, The Urlparameter.optional property depends on the parameter parameter defined in the rule, which represents an optional read-only parameter if some of the parameters in the routing rule are not specified in the actual URL, the default value is used as the value of the parameter
defaults:new {controller = "Home", action = "Index", id = urlparameter.optional}
), the debugging of the route, we can use Routedeugger to debug the route when we develop, see if we write the route has the function, use the following to add the RouteDebug.dll reference, after registering the route in Global.asax.cs to add RouteDebug.RouteDebugger.RewriteRoutesForTesting (routetable.routes);
MVC Routing Rules (Custom, constraint, Debug)