II. rewritepath method of ihttpmodule
How can I come by myself? Can I write an isapi in c ++? A: No.
I wrote it honestly with. net.
That's easy: From global. asax moved to ihttphandle for a long time. After a wrong direction, it was still in the same place. Then it was discovered after a local search. The path had to go to ihttpmodule.
It is easy to use ihttpmodule. Many people use this as a result. Write a class that inherits from ihttpmodule and register it in the configuration file. The example is as follows:
1: Create a class library project: Name: urlrewrite
2: Add Reference system. web, because the ihttpmodule is in this namespace, and the class library does not reference this by default.
3: rename class1.cs to urlrewrite. cs, and let the class inherit from ihttpmodule to implement the interface as follows:
Adjust the code as follows:
# Region ihttpmodule member
Public void dispose ()
{
// Throw new exception ("the method or operation is not implemented .");
}
Public void init (httpapplication context)
{
Context. beginrequest + = new eventhandler (context_beginrequest );
}
Void context_beginrequest (object sender, eventargs e)
{
Httpapplication app = (httpapplication) sender;
Httpurlrewrite (app. context );
}
Public void httpurlrewrite (httpcontext context)
{
String url = context. request. url. tostring (); // The url requested by the user
// -- Perform a lot of url logic processing here
Context. rewritepath ("~ /Default. asp tutorial x ", null," url = "+ url );
}
# Endregion
The above code indicates:
Send the whole request url as a parameter to the default. aspx page. All requests are converted to default. aspx? Url = www.cyqdata.com in a similar way
Next, create another site to accept and output the result:
4. Add the new website name urlrewritedemo.
Pstutorial: The app was originally to be created. If you cannot open the example when downloading it, the sample is in the form of a website.
5. Add reference to the urlrewrite project
6: f5, call up web. config, and add the configuration item code in the configuration file as follows:
<Httpmodules>
<Add name = "urlrewrite" type = "urlrewrite. urlrewrite, urlrewrite"/>
</Httpmodules>
The following is an example of the current situation:
Then write a line of code to default. aspx to output the received parameters:
Protected void page_load (object sender, eventargs e)
{
Response. write (request ["url"]);
}
Everything is ready and running. Let's look at the results:
Figure 1: the default page of the request. The output result indicates that the page is normal.
Figure 2: If you request a page that does not exist, the output result is normal.
Figure 3: request a url without a suffix and output the result, which is a bit surprising
As shown in figure 3, it seems that iis integrated with vs2005 processes all requests in a unified manner, so it can be processed even without a suffix.
Important notes:
For iis Site development or final deployment, the default aspnet_isapi.dll cannot handle suffixes without suffixes or images. Therefore, you need to add extended generic mappings to achieve this function.