The simplest method to implement URL rewriting (URLRewriter) in asp.net.
I have referenced the masterpiece (translated by Scott Mitchell: Janssen). Although I have not fully understood it, I have also made a sense of accomplishment. Write it out and share it.
I have talked a lot about the principles in the original article, so I won't talk about it here (I don't understand it either ). Write the operation process here. The purpose is to implement the simplest program that can implement URL rewriting.
1. Set site properties in IIS.
2. modify the content of web. config.
Copy codeThe Code is as follows:
<System. web>
<HttpHandlers>
<Add verb = "*" path = "*. zhtml" type = "ZDIL. URLRewriter. RewriterFactoryHandler, ZDILURLRewriter"/>
</HttpHandlers>
</System. web>
*. Zhtml is the extension of the webpage written in the address bar, which is displayed to users. This can be changed at Will (but it must comply with the extension rules !). Of course, it must be consistent with the settings in step 1.
3. Write a class.
Code
Copy codeThe Code is as follows:
Using System;
Using System. IO;
Using System. Web;
Using System. Web. UI;
Namespace ZDIL. URLRewriter
{
/** // <Summary>
/// URL rewriting
/// </Summary>
Public class RewriterFactoryHandler: IHttpHandlerFactory
{
/** // <Summary>
/// GetHandler is executed by the ASP. NET pipeline after the associated HttpModules have run. The job
/// GetHandler is to return an instance of an HttpHandler that can process the page.
/// </Summary>
/// <Param name = "context"> The HttpContext for this request. </param>
/// <Param name = "requestType"> The HTTP data transfer method (<B> GET </B> or <B> POST </B>) </param>
/// <Param name = "url"> The RawUrl of the requested resource. </param>
/// <Param name = "pathTranslated"> The physical path to the requested resource. </param>
/// <Returns> An instance that implements IHttpHandler; specifically, an HttpHandler instance returned
/// By the <B> PageParser </B> class, which is the same class that the default ASP. NET PageHandlerFactory delegates
/// To. </returns>
Public virtual IHttpHandler GetHandler (HttpContext context, string requestType, string url, string pathTranslated)
{
String sendToUrl = url; // address in the address bar
String filePath = pathTranslated;
String sendToURLString = "/web/index. aspx"; // the page to be accessed
String queryString = ""; // parameter. For example? Id = 123
FilePath = context. Server. MapPath (sendToURLString); // physical address
// This sentence is the most important. Turning.
Context. RewritePath (sendToURLString, String. Empty, queryString );
Return PageParser. GetCompiledPageInstance (url, filePath, context );
}
Public virtual void ReleaseHandler (IHttpHandler handler)
{
}
}
}
This class should be written in a separate project and then compiled into the ZDILURLRewriter. DLL file. (Note the file name. If the file name is incorrect, it cannot run properly ).
4. Finished.
Open IE and enter http: //.../1. zhtml in the address bar.
The viewer sees a static page address, but actually accesses the dynamic page/web/index. aspx.
It's easy.
Of course, this is the simplest way to be "unusable. Because it will "Overwrite" All *. zhtml access to/web/index. aspx.
As for what kind of web page is rewritten to which web page, we will not introduce it here (Here we only talk about methods, not the implementation details ).
There are many methods. The original works are matched by regular expressions. I used string sendToUrl = url; to judge.
The rest depends on your needs.