What is ihttphandler? On the surface, it is an interface. In fact, it can process the requests submitted by the client and return them to the client.
The ihttphandler interface includes the isreusable attribute and processrequest method. The isreusable attribute indicates whether it can be reused. The processrequest method is used to process client requests.
Some people in the group raised the need to rewrite the URL, so they can help implement it without any worries. The brother's requirements are as follows:
Enter the following format
Http: // localhost: 2868/cgi/redirectform? Uid = pxl & Pwd = 12346
You can see that the actual page displayed for URL rewriting is
Redirectform. aspx under Handler
First, let's take a look at the configuration information in Web. config:
<Httphandlers>
<Add verb = "*" Path = "cgi/*" type = "myhttphandler. urlchangehandler, myhttphandler"/>
1) verb indicates the client request type. "*" indicates any request type. Of course, it can also be "Get, head", "post, get", etc.
2) path indicates the Request Path of the client. This is very useful, and the response of the server control can be used (subsequent verification code Server ControlArticleThe path is set to "cgi/*", which means that if the path request is sent by the client, "*" indicates any string, such as cgi/test, CGI/123456, CGI/redirectform? Uid = pxl & Pwd = 123456.
3) type is a class in the format of [namespace]. [processing class name], [namespace] ". The processing class name is the noun that implements the ihttphandler interface.
4) In general, this configuration indicates that the client sends a request of the verb type "*" (any type of request), and The Request Path format is "cgi /*", then the urlchangehandler class will process the data on the server.
The URL rewriting class can be written as a DLL. This dll can be introduced when URL rewriting is required and configured in Web. config.
Next we will implement our processing class:
Using System;
Using System. Collections. Generic;
Using System. text;
Using System. Web;
Using System. Web. UI;
NamespaceMyhttphandler
{
Public ClassUrlchangehandler: ihttphandler
{
# RegionIhttphandler members
Public BoolIsreusable
{
Get{Return True;}
}
Public Void Processrequest (httpcontext context)
{
If (Context. Request. Path. indexof ( " Aspx " ) ! = - 1 )
{
Return ;
}
Stringbuilder sbobj = New Stringbuilder ();
Sbobj. append (context. Request. Path );
Sbobj. append ( " . Aspx? " );
Sbobj. append (context. Request. querystring. tostring ());
String URL = Sbobj. tostring (). Replace ( " CGI " , " Handler " );
Context. server. Transfer (URL, True );
}
# Endregion
}
}
The processrequest method has an httpcontext that contains HTTP request information.
If (context. Request. Path. indexof ("aspx ")! =-1)
{
Return;
}
This is to determine the Request Path. If the request page is normal, it will not be processed. For example, the Request Path is "http: // localhost: 2868/handler/redirectform. aspx ". If the Request Path complies with "cgi/*", process the request:
Stringbuilder sbobj = new stringbuilder ();
Sbobj. append (context. Request. Path );
Sbobj. append (". aspx? ");
Sbobj. append (context. Request. querystring. tostring ());
String url = sbobj. tostring (). Replace ("cgi", "handler ");
Context. server. Transfer (URL, true );
Use the stringbuilder class to rewrite the Request Path. Assume that the input path is "http: // localhost: 2868/cgi/redirectform? Uid = pxl & Pwd = 123456 ". The process is as follows:
1) first obtain "context. Request. Path", that is, "http: // localhost: 2868/cgi/redirectform ".
2) then append ". aspx", that is, "http: // localhost: 2868/cgi/redirectform. aspx ".
3) then add the passed parameter "http: // localhost: 2868/cgi/redirectform. aspx? Uid = pxl & Pwd = 123456 ".
4) Replace "cgi" in the path with "handler", that is, "http: // localhost: 2868/handler/redirectform? Uid = pxl & Pwd = 123456 ".
5) Finally, use the context server. Transfer in the browser without changing the path "http: // localhost: 2868/cgi/redirectform? When uid = pxl & Pwd = 123456 ", the URL is rewritten. The actual conversion is" http: // localhost: 2868/handler/redirectform? Uid = pxl & Pwd = 123456 ".
Well, simple URL rewriting has been completed. Let's try it.