ASP. NET Routing

Source: Internet
Author: User

First, if you need to use the ASP. NET Routing function in the project, you need to configure an HttpModule in the web. config file:

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

Second, you should add a series of RouteBase objects to the RouteCollection type RouteTable. Routes set in Application_Start, and specify an independent name (case-insensitive) for each RouteBase object ). Of course, you can also dynamically add or delete content at runtime (RouteCollection objects are thread-safe), but we do not usually do this. It is worth noting that the order of RouteBase objects in RouteCollections is very important.

The UrlRouteModule listens to the PostResolveRequestCache event of ASP. NET Request Pipelines. In this event, the UrlRouteModule uses the current HttpContext as the parameter to call the GetRouteData method of the RouteTable. Routes set. In the GetRouteData method of RouteCollection, HttpContext is successively passed into the GetRouteData method of each RouteBase object. If a RouteBase object returns a non-null result in the middle, then the result is directly returned to the UrlRouteModule.

If the UrlRouteModule calls the RouteTable. Routes. GetRouteData method to get null, "Everything has never happened ". If the GetRouteData method returns the result-a RouteData object, the RouteData. Values will contain the data captured in the request. Another important member in RouteData is the RouteData. RouteHandler attribute, which returns an IRouteHandler object. The IRouteHandler interface has only one GetHttpHandler method. It accepts RequestContext as the parameter and returns an IHttpHandler object. For example, when ASP. net mvc framework uses ASP. NET Routing, MvcRouteHandler is used to return an MvcHandler object.
However, after the UrlRouteModule obtains the IRouteHandler object, it does not directly call its GetHttpHandler method, but judges whether it is the StopRoutingHandler type inherent in ASP. NET Routing. StopRoutingHandler is a special IRouteHandler object. Its function is only to tell UrlRouteModule that although a rule is successfully matched, it still means nothing has happened. Therefore, if we want to "Skip" some forms of requests, we usually need to put the "Ignore" function 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 defined in ASP. net mvc Based on the RouteCollection type. It will add a Route object to the RouteCollection, And the RouteData object returned when the Route object matches successfully, its RouteHandler attribute will be a StopRoutingHandler, therefore, the remaining Routing rules will not continue to match-this is different from the null returned by the RouteBase object, because if null is returned, the remaining Rules will match in sequence. If a RouteData containing StopRoutingHander is returned, all the remaining Routing rules are skipped.


If the IRouteHandler object obtained by UrlRouteModule is not StopRoutingHandler, The IHttpHandler object will be obtained through its GetHttpHandler method. This IHttpHandler object will be placed in the Items set of HttpContext. So far, the Request Pipeline's PostResolveRequestCache event has ended.

The UrlRouteModule also listens to the PostMapRequest event, and the Module looks for HttpContext. whether the specified position of the Items set contains an IHttpHandler object. If yes, this object is set to the value of the Handler attribute of the current HttpContext object. So when ASP. NET continues to execute, the Handler's ProcessRequest method will be called to process the request.

If the IHttpHandler object is MvcHttpHandler, it obtains some data from RouteData, constructs the Controller object, and executes the Action. If it is a DynamicDataHandler or WebForm HttpHandler, the rest is the processing method of the respective models. (Www.bkjia.com)

Therefore, ASP. NET Routing is a common component that does not involve any specific request processing methods. If you need it, you can also develop it on your own-such as the FubuMvc project.

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.