Asp.net URL routing (system. Web. Routing. dll)

Source: Internet
Author: User

System. web. routing is. net 3.5sp1 adds a DLL. The class that uses it to arch can easily implement URL ing, in Asp.net webfrom programming, the URL requested by the client can become more "cool". Of course, its biggest role should be to serve the Asp.net MVC framework.

The implementation principle of urlrouting is actually not complicated. web. the routing space has a class urlroutingmodule which implements the ihttpmodule interface and subscribes to the postresolerequestcache and postmaprequesthandler events of the httpapplication object, in the first event, the urlroutingmodule prepares an httphandler for this request and sends the httphandler to httpcontext In the second event. The code for postresolverequestcache is as follows:

Private void onapplicationpostresolverequestcache (Object sender, eventargs E)
{
Httpcontextbase context = new httpcontextwrapper (httpapplication) sender). context );
This. postresolverequestcache (context );
}

Public Virtual void postresolverequestcache (httpcontextbase context)
{
Routedata = This. routecollection. getroutedata (context );
If (routedata! = NULL)
{
Iroutehandler routehandler = routedata. routehandler;
If (routehandler = NULL)
{
Throw new invalidoperationexception (string. Format (cultureinfo. currentuiculture, routingresources. urlroutingmodule_noroutehandler, new object [0]);
}
If (! (Routehandler is stoproutinghandler ))
{
Requestcontext = new requestcontext (context, routedata );
Ihttphandler httphandler = routehandler. gethttphandler (requestcontext );
If (httphandler = NULL)
{
Throw new invalidoperationexception (string. Format (cultureinfo. currentuiculture, routingresources. urlroutingmodule_nohttphandler, new object [] {routehandler. GetType ()}));
}
Requestdata data2 = new requestdata ();
Data2.originalpath = context. Request. path;
Data2.httphandler = httphandler;
Context. items [_ requestdatakey] = data2;
Context. rewritepath ("~ /Urlrouting. axd ");
}
}
}
Code Description:

1. httpcontextbase is an abstract class whose members are exactly the same as httpcontext. httpcontextwrapper inherits from httpcontextbase and uses the httpcontext object as a member of itself, its method implementation relies entirely on the httpcontext object. Httpcontextbase and httpcontextwrapper are both added to system. Web. abstractions. dll. This dll is also added to. Net SP1. It is mainly used for Asp.net unit testing.

2. routedata is a route data that corresponds to the current request URL configuration. The URL routing class route is configured and added to the routes set of routetable. routetable uses the singleton design mode, therefore, it is thread-safe and the entire application shares a routetable object. Therefore, to add a routetable, it must be in global. asax application_start event.

3. obtain a routehandler object from routedata. This object is implemented by the iroutehandler interface. net SP1 does not have the iroutehandler implementation that can be directly used, so we need to implement it ourselves to create an available httphandler for the current route.

Code Implementation of postmaprequesthandler:

Private void onapplicationpostresolverequestcache (Object sender, eventargs E)
{
Httpcontextbase context = new httpcontextwrapper (httpapplication) sender). context );
This. postresolverequestcache (context );
}

Public Virtual void postmaprequesthandler (httpcontextbase context)
{
Requestdata DATA = (requestdata) Context. items [_ requestdatakey];
If (Data! = NULL)
{
Context. rewritepath (data. originalpath );
Context. Handler = data. httphandler;
}
}

Code Description: The httphandler object obtained above is handed over to the handler attribute of httpcontext.

To use urlroute in Asp.net webfrom, you must complete three tasks:

1. Implement the ihttphandler Interface

2. Implement the iroutehandler interface and return the corresponding ihttphandler object according to the routedata attribute of requestcontext

3. Configure the route to assign the iroutehandler object to the handler of the route object, and add the route to the routes set of the routetable

Next we can use urlrouting in Asp.net webfrom and make our URL "cool" (to be continued ......)

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.