ASP. NET MVC API routing generation rules

Source: Internet
Author: User

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:

    1. Parameter information passed in when an ASP. NET MVC API function is called

    2. 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)

    3. 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

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.