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 goal is to achieve a simple implementationURL rewritingOfProgram.
1,You need to set site properties in IIS.
2,Modify web. config content.
< System. Web >
< Httphandlers >
< Add Verb = "*" Path = "*. Zhtml" Type = "Zdil. urlrewriter. rewriterfactoryhandler, zdilurlrewriter" />
</ Httphandlers >
</ System. Web >
Where*. ZhtmlIt is the extension of the webpage written in the address bar, which is displayed to the user. 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
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 <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 " ; // Page to be accessed
String Querystring = "" ; // Parameters. For example? Id = 123
Filepath = Context. server. mappath (sendtourlstring ); // Physical address
//This sentence is the most important. Turning.
Context. rewritepath (sendtourlstring, String. Empty, querystring );
ReturnPageparser. getcompiledpageinstance (URL, filepath, context );
}
Public Virtual VoidReleasehandler (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 it in the address bar.Http: //.../1. zhtml.
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.