We all know that some API functions that call ASP. NET MVC (such as: Url.action, redirecttoaction, etc.) can generate url,asp.net MVC matches the system-defined route (route) based on the parameters passed in when the API function is called. The corresponding URL is then generated by matching the successful route.
ASP. NET MVC generates a URL in turn based on the following three rules:
Parameter information passed in when an ASP. NET MVC API function is called
The matching value of the currently requested URL (that is, Request.url) and the system defines the route match (in the order defined by the routing table, from the top down)
The system defines the default values for routing parameters in the route
Here's an example of how ASP. NET MVC generates URLs from the above three points.
First, we defined the following two routes in the MVC system
Public classrouteconfig{ Public Static voidregisterroutes (routecollection routes) {routes. Ignoreroute ("{Resource}.axd/{*pathinfo}"); Routes. MapRoute (Name:"Lang", URL:"Culture/{lang}/{controller}/{action}/{id}", defaults:New{controller ="Home", action ="Index", id =123 } ); Routes. MapRoute (Name:"Default", URL:"{Controller}/{action}/{id}", defaults:New{controller ="Home", action ="Index", id =urlparameter.optional}); }}
When you run an ASP. NET MVC project, if we enter the address Http://localhost/culture/en-us/Home/Index on the browser address bar, this address will match the first route Lang above, because the ASP. The MVC routing table is matched from top to bottom, and if a route matches successfully it will no longer match.
If we are now on the view Http://localhost/culture/en-us/Home/Index this address (that is, the index view under HomeController), Call url.action to generate the URL:
<!DOCTYPE html><Html><Head><MetaName= "Viewport"Content= "Width=device-width"/><Title>index</title< Span style= "color: #0000ff;" >></head>< Span style= "color: #0000ff;" ><body> << Span style= "color: #800000;" >div> @Url. Action ("dosometing") </div></< Span style= "color: #800000;" >body></html< Span style= "color: #0000ff;" >>
So let's take a look at the steps that ASP. NET MVC generates URLs when invoking the API:
1. Because the parameter dosomething is passed in when the API Url.action is called, the parameter action is dosomething in the route.
Action:dosomething
2. The current request URL is Http://localhost/culture/en-us/Home/Index, then use this address from top to bottom to match the system-defined route, matching to the first route Lang when we can get two parameter information, Controller for Home,lang is en-us, so now we get the routing information is:
Controller:homecontroller
Action:dosomething
Lang:en-us
3. Because of Route Lang, we have defined the parameter ID with a default value of 123, so we finally get the following routing information:
Controller:homecontroller
Action:dosomething
Lang:en-us
Id:123
Now based on the resulting routing information and then go from top to bottom to match the system-defined routes, then first match to Route Lang, MVC found that each route parameter in Lang now has a value, matching success, So the URL generated based on Lang is as shown (note: The parameter ID is not reflected in the generated URL because it is the default value defined in the route lang)
ASP. NET MVC API routing generation rules