Asp.net URL rewriting Method -- using Routing

Source: Internet
Author: User
Using Routing

 

SeriesArticleDirectory

I. pseudo-static mode (using urlrewriter)
1. Rewrite method (article address: http://www.cnblogs.com/scottckt/archive/2011/01/12/1933737.html)
2. Processing Method for sending back (article address: http://www.cnblogs.com/scottckt/archive/2011/01/12/1933791.html)
3. Rewrite aspx to HTML method (article address: http://www.cnblogs.com/scottckt/archive/2011/01/12/1933836.html)

2. Real rewriting (using system. Web. Routing) (article address: http://www.cnblogs.com/scottckt/archive/2011/01/12/1933893.html)

 

 

1. Introduce system. Web. RoutingProgramSet

System. Web. Routing assembly. Net framework3.5. Add reference. After introduction, you can see that a row is added in <assemblies> of Web. config. If not, manually add the row. < Add Assembly = "System. Web. Routing, version = 3.5.0.0, culture = neutral, publickeytoken = 31bf3856ad364e35" /> </ Assemblies >

Ii. Add urlroutingmodule Configuration
System. Web. routing is implemented based on httpmodules. Therefore, add the following to the


Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/



--><AddName= "Urlroutingmodule"Type= "System. Web. Routing. urlroutingmodule, system. Web. Routing, version = 3.5.0.0,

Culture = neutral, publickeytoken = 31bf3856ad364e35"/>

3. Register URL routing rules when the program starts

This must be controlled in global. asax and a route entry should be registered in the application_start () method. If you understand the TCP/IP route, it is easy to understand the route here. The TCP/IP network route refers to the gateway to which the data packet goes, and the route refers to the iroutehandler instance to which the actual resource is accessed.

Registered route of Global. asaxCodeAs follows:

 
UsingSystem. Web. Routing;
Using
Useful;

Protected VoidApplication_start (ObjectSender, eventargs E)
{
Registerroutes (routetable. routes );
}

# Region Custom Method
///   <Summary>
/// Register URL Routing
///   </Summary>
///   <Param name = "Routes"> </param>
/// Test: Http: // localhost : 5876/test-11 view results
Private   Void Registerroutes (routecollection routes)
{
Iroutehandler routerhander
=   New Rssroutehandler ();
Route dealedroute
=   Null ;
// The URL of the/type-num rule will be forwarded to the rssroutehandler instance for processing.
Dealedroute =   New Route ( " {Type}-{num} " , Routerhander );
Routes. Add (dealedroute );
}
# Endregion

 

URL rules are similar to those in URL rewriter. It may be represented in a regular expression. . The URL that complies with the rule is forwarded to the iroutehandler associated with it for processing. Note what the routevaluedictionary parameters represent, and how they are declared, how to set regular expressions, and how they correspond to parameters. When registering a route, you can add other constraints, such as processing only post requests.

Iv. iroutehandler implementation

Since we have previously implemented iroutehandler ing for URLs, let's take a look at what rssroutehandler looks like. I put the rssroutehandler class in Useful Project. Using System. Web. Routing;
 
Namespace Useful
{
Public   Class Rssroutehandler: iroutehandler
{
Public Rssroutehandler ()
{
}
Public System. Web. ihttphandler gethttphandler (requestcontext)
{
Return   New Rssprovider (requestcontext );
}
}
}


You only need to implement the iroutehandler interface and the interface method. The interface method must return an ihttphandler implementation instance. The requestcontext parameter is required in your ihttphandler implementation class. In this way, the route is completed from the URL-> iroutehandler-> ihttphandler layer. People who have written httphandler should know that it can process what the page can do. Let's take a look at the simple implementation of rssprovider here:

5. ihttphandler implementation Using System;
Using System. Web;
 
Namespace Useful
{
Public   Class Rssprovider: ihttphandler
{
Public System. Web. Routing. requestcontext { Get ; Set ;}
Private   Bool Haveparamter =   False ;
Public Rssprovider (system. Web. Routing. requestcontext context)
{
This . Requestcontext = Context;
}
 
Public   Bool Isreusable
{
Get
{
Return   False ;
}
}
 
Public   Void Processrequest (httpcontext context)
{
Dealparamter (context );
}
 
Private   Void Dealparamter (httpcontext context)
{
String Type =   This . Requestcontext. routedata. Values [ " Type " ]. Tostring ();
Int Num = Int32.parse ( This . Requestcontext. routedata. Values [ " Num " ]. Tostring ());
Context. server. Execute (
" Test. aspx? Type = "   + Type +   " & Num = "   + Num );
Context. response. Write (
" Type: "   + Type +   " <Br/> Number: "   + Num +   " <Br/> " );
Context. response. Write (
" Set the Response Header according to the preceding conditions to output the required data. " );
}
 
}
}


You can obtain the request parameters from the rewritten URL according to the rules, and then transfer them to the actual resource for processing, or directly call other methods in the httphandler processrequest () method to play the cost of the request.

The requestcontext instance is injected into the httphandler implementation class rssprovider in rssroutehandler. You can use this instance to obtain the desired parameters from the URL. In addition, in practical applications, it is best to clarify the reuse of iroutehandler and ihttphandler implementation class instances, and you can choose an appropriate design mode to ensure.

6. If it is IIS 6 in win2003, other unknown request types are configured and processed by ASP. NET.

No special settings are required for IIS 6 in XP or IIS 7 on any platform. Default web site attributes, in the Home Directory tab, configuation ..., insert the value of wildcard application maps (order of implementation) to c: \ windows \ microsoft.net \ framework \ v2.0.50727 \ aspnet_isapi.dll. If verify that file exists is not checked.


In this way, rules such as/type-num are executed to the processrequest () method of rssprovider, and the real URL is rewritten, the aspx file is not required to process HTTP requests.

 

 

Codeplex has an example similar to this example, but is encapsulated in some places. : Http://aspnet35routing.codeplex.com/SourceControl/list/changesets

 

Reference: http://school.ty502.com/wz_4563.html

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.