The processing method of ASP.net routing to request

Source: Internet
Author: User
Tags httpcontext

First, if you need to use the ASP.net routing feature in your project, you need to configure a httpmodule in the Web.config file:

<add name= "UrlRoutingModule" type= "System.Web.Routing.UrlRoutingModule, System.Web.Routing, ..."/>

Second, you should be in Application_ In Start, add a series of RouteBase objects to the Routetable.routes collection of the RouteCollection type and specify a separate name (regardless of capitalization) for each RouteBase object. Of course, you can also add or remove content dynamically at run time (the RouteCollection object is thread safe), but we don't usually do it. It is noteworthy that the order of the RouteBase objects in the routecollections is very important.

Urlroutemodule listens to the Postresolverequestcache event of the ASP.net Request pipelines, in which Urlroutemodule will present the current HttpContext The Getroutedata method that calls the Routetable.routes collection as a parameter. In the RouteCollection Getroutedata method, the HttpContext is then passed into the Getroutedata method of each RouteBase object in turn, If a RouteBase object returns a non-null result in the middle of the way, the result is returned directly to Urlroutemodule.

If Urlroutemodule calls the RouteTable.Routes.GetRouteData method to get null, then "everything seems to have never happened." If the Getroutedata method gets the result--a Routedata object--then Routedata.values contains the data captured in the request. Another important member of the Routedata is the Routedata.routehandler property, which returns a Iroutehandler object. There is only one method Gethttphandler in the Iroutehandler interface, which takes requestcontext as an argument and returns a IHttpHandler object. If the ASP.net MVC framework uses the ASP.net routing, Mvcroutehandler is used to return a Mvchandler object.
However, after the Iroutehandler object is obtained, Urlroutemodule does not call its Gethttphandler method directly, but rather determines whether it is asp.net Routing the Stoproutinghandler type of the band. Stoproutinghandler is a special Iroutehandler object, its role is to tell urlroutemodule, although a rule match succeeded, but--also when nothing happened. Therefore, if we want to "skip" some form of request, it is often necessary to place the "ignore" feature before all other rules. For example,

public static void RegisterRoutes (RouteCollection routes)
{
Routes. Ignoreroute ("{resource}.axd/{*pathinfo}");
Routes. Ignoreroute ("Scripts/{*pathinfo}");
Routes. Ignoreroute ("Images/{*pathinfo}");
 
Routes. Maproute (
' Default ',//Route name
' {controller}/{action}/{id} ',//URL with parameters
New {controller = "Home", action = "Index", id = ""}//Parameter defaults
);
}

Ignoreroute is an extension method that is defined in ASP.net mvc based on the routecollection type. It adds a route object to the RouteCollection, and the Routehandler property of the route object returned when the match succeeds is a stoproutinghandler, The remaining routing rules will not continue to match-this is different from the RouteBase object returning NULL because if NULL is returned, the remaining rules will be matched sequentially. If a routedata containing Stoproutinghander is returned, the remaining routing rules are skipped.


If the Iroutehandler object obtained by Urlroutemodule is not Stoproutinghandler, then the IHttpHandler object is obtained by its Gethttphandler method. This IHttpHandler object is placed in the HttpContext items collection. At this point, the Request pipeline Postresolverequestcache event is over.

Urlroutemodule also listens for postmaprequest events, The module then looks for whether a IHttpHandler object is contained in a specific location in the Httpcontext.items collection and, if it exists, sets the object to the value of the handler property of the current HttpContext object. So when the ASP. NET continues to execute, this handler ProcessRequest method is invoked to process the request.

If the IHttpHandler object is Mvchttphandler, it takes some data from the Routedata, constructs the controller object, executes the action, and so on. If it's a dynamicdatahandler, or a WebForm HttpHandler, then the rest is the way the model is handled. (www.3lian.com)

Therefore, asp.net routing is a generic component that does not involve any specific request processing. If you need it, you can develop it yourself based on it--as the FUBUMVC project does.

Related Article

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.