What is URL routing?
Theoretically, the small part is not more said, only a simple introduction, their own understanding.
The so-called URL routing, is a rule. By this rule to match. Just like the regular expressions we often use.
The default URL route
If you open RouteConfig.cs in the project, you will see the following code
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" ><span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >public static void RegisterRoutes (RouteCollection routes) { routes. Ignoreroute ("{resource}.axd/{*pathinfo}"); Routes. MapRoute ( name: "Default", //route name URL: "{controller}/{action}/{id}",// parameter Match table //default parameter defaults:new {controller = "Home", action = "Index", id = urlparameter.optional} ); } </span></span>
All of the ASP. NET MVC routing will be defined here. Where Routable.routes is a public static object that stores all the routing rule sets
Routing Analysis
Read the above default routing information, the following to read.
1. Each routes represents a matching rule.
2.IgnoreRoutes: Defines URLs that do not require matching routing processing.
3.resource: A random name, used to match URLs like **.axd
4.{*pathinfo}:* is what we often see, representing all, taking all the meaning. For example, if the URL is/test.axd/b/d/e, then {pathInfo} takes the table b/d/e. But if you do not take *, you will only get the position of the B variable.
5.MapRoute is called defining a default routing rule
Give it a try.
Take a look at the explanations above and try some examples.
example one: The URL is http://localhost/Test.axd/b/c/d/e and the Http://localhost/Test.axd
1. Compare the Routes.ignoreroute to the first. It is just right to find the two by comparison, except that the {*patchinfo} in the latter is empty. So HTTP requests are serviced by this URL.
example two: URL is Http://localhost/Home/Login?id=3
1. Starting from top to bottom, the Routes.ignoreroute rule does not pass, so the rule of Routes.maproute is started.
2. By comparison {Controller}/{action}/{id}, we can get Controller=home,action=login, and? Id=3 is not in this rule.
3. Because the rule is divided by a slash (/), the id=123 is less than right but can read to the default value urlparamete.optional.
Routing extensions
As the above analysis, we know the rules of routing, so we can also customize their own rules.
Regular expressions
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >//Routing constraint public static void RegisterRoutes (RouteCollection routes) { routes. Ignoreroute ("{resource}.axd/{*pathinfo}"); Routes. MapRoute ( name: "Default", //route name URL: "{controller}/{action}/{id}",// parameter Match table //default parameter defaults:new {controller = "Home", action = "Index", id = ""}, //Add restriction, only allow controller to start with H constraints:new {C Ontroller= "^h.*"} ); </span>
The above adds a restriction to routing by adding contraints to the route, allowing only the start with H.
URL routing There are other uses, interested, more research and sharing.
URL Routing in MVC