Global analysis of the operating mechanism of ASP.--url

Source: Internet
Author: User

Global

First, let's look at a picture.

First, the user sends a URL request to the server through a Web browser, where the requested URL is no longer in the xxx.aspx format, but http://HostName/ControllerName/ActionName/Parameters. This request was intercepted by the route mapping system of ASP. (The route map can be configured in Global.asax, we'll talk about it later) the route Mapping system resolves the controller name Controllername,action name actionname and each parameter parameters according to the mapping rules, then finds con in Controllers directory. TrollerNameController.cs this controller class, by default, the system is always looking for the "controller name +controller" Under the Controllers directory, and then, looking for a method under this class with the same name as ActionName, find the , the parameters is passed as an argument to this method, and then the action method begins execution, returns the corresponding view when finished, and by default returns an ASPX file with the same name as ActionName under the directory with the same name as Controllername under the views directory. and pass the ViewData to the view. The ViewData typically contains the controls that control the view display and the data that the view needs to display.
We review the previous page's request process according to the above ideas. The URL we passed is http://localhost/Home/Index. Under the default routing rule, set Controllername to "Home" and ActionName to "Index" without parameters. So the system to find the controllers directory of the HomeController class index method, successfully found, so executed. This method calls the mock model to take out some data and put it into the ViewData corresponding key value entry. Then return to the view and return to the next home under the index.aspx. This view takes out the data in the ViewData in a certain format and completes a typical ASP. NET MVC call.

Routing
As you can see from the above, routing is important in ASP. It directly determines how the URL is parsed, so it determines how the system works. So, let's uncover the mystery of routing.
Open our demo under the Global.asax.cs file, you can see the following code:
usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingsystem.web; usingSYSTEM.WEB.MVC; usingSystem.Web.Routing; namespaceMvcdemo {//note:for instructions on enabling IIS6 or IIS7 Classic mode,//Visithttp://go.microsoft.com/?LinkId=9394801         Public classMvcApplication:System.Web.HttpApplication { Public Static voidregisterroutes (routecollection routes) {routes. Ignoreroute ("{Resource}.axd/{*pathinfo}"); Routes. MapRoute ("Default",//Route name                "{Controller}/{action}/{id}",//URL with Parameters                New{controller ="Home", action ="Index", id =""}//Parameter Defaults            ); }            protected voidApplication_Start () {registerroutes (routetable.routes); }      }  } 

Notice there's a routes. Maproute method. The function of this method is to add a routing rule to the system. The only rule here is that the system is incremented by default, and the first parameter is the rule name, which is a normal string. The key is the second argument, which is also a string, but it describes how to parse the URL. It can be understood that it describes how the URL string hostname after the next section, where the expression parameter with {} matches, if not with the string match.
For example, the above {Controller}/{action}/{id} indicates that if there is a string of three segments separated by "/" after hostname, the URL is matched and parsed into the controller name, the action name, and a parameter called "id". If you enter HTTP://LOCALHOST/HOME/INDEX/1 then "1" will be treated as the value of the parameter ID, but if you request HTTP://LOCALHOST/HOME/INDEX/1/2, sorry, your request cannot succeed, Because this routing rule does not match your URL, because there are four segments behind your hostname, this routing rule can only match three segments.
Perhaps you also notice a problem, Http://localhost/Home/Index clearly hostname behind only two paragraph, how also was matched? This is the third parameter of the Maproute method that works. The function of this parameter is to set the default value for each {} match segment in the above rule, as above, the default value for ID is "", or null. So in Http://localhost/Home/Index, although the specified ID is not displayed, it can still be matched successfully, by default as a null value. If you remove the id= "", you will find that Http://localhost/Home/Index is no match. By analogy, http://localhost/Home/can also match successfully because {action} defaults to index,http://localhost/and can match successfully because the default {controller} is Home, so, Under this default value, Http://localhost/Home/Index and http://localhost/are equivalent.
In the analysis, we come to an important conclusion: in the case where the default value is set, the mapping rule is "less than", and the less part is replaced by the default value.

In the above matching rule, three matching segments have curly braces, all of which are parameter matching, and we say strong string matching. For example, we have a URL that needs to be http://localhost/Category/Detail/Name. If you follow the above matching rule, the value of the Name field will be matched to the ID, but we would like to use a parameter called "name" in Categorycontroller's detail method instead of using a parameter called "id". Quite simply, let's add a matching rule:

usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingsystem.web; usingSYSTEM.WEB.MVC; usingSystem.Web.Routing; namespaceMvcdemo {//note:for instructions on enabling IIS6 or IIS7 Classic mode,//Visithttp://go.microsoft.com/?LinkId=9394801         Public classMvcApplication:System.Web.HttpApplication { Public Static voidregisterroutes (routecollection routes) {routes. Ignoreroute ("{Resource}.axd/{*pathinfo}"); Routes. MapRoute ("Category",//Route name                "Category/detail/{name}",//URL with Parameters                New{controller ="Category", action ="Detail", name =""}//Parameter Defaults            ); Routes. MapRoute ("Default",//Route name                "{Controller}/{action}/{id}",//URL with Parameters                New{controller ="Home", action ="Index", id =""}//Parameter Defaults            ); }            protected voidApplication_Start () {registerroutes (routetable.routes); }      }  }

As you can see, we added a rule before the default rule, where the controller name and action name are no longer arguments and become strong strings (without {}). At this point, when the URL we request is Http://localhost/Cateogry/Detail/para, the new rule will be matched directly, and the value of para will not be assigned to the ID but assigned to the variable named name.
It is important to note that our new routing rules must be preceded, because ASP. NET MVC will match the matching routing rule found from the top down to the first one.

View
When we're done with the routing rules, we'll talk about the view.
It says that the action method return type is ActionResult, in fact, this return type is not limited to the view method return Viewresult, it also has a lot of implementations, here are a few.
ViewResult: An aspx file is typically rendered and returned by the view method.
Redirecttoresult: Redirects the browser, returned by the redirect method.
Redirecttorouteresult: Directly to the next action, returned by the Redirecttoaction method.
There are a few, not to mention, because the follow-up article basic use of the other, about that several friends can see their own information.


Original link: Global analysis of the operating mechanism of ASP.--url

Global analysis of the operating mechanism of ASP.--url

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.