ASP. net url rewrite. URL rewriting
I. URL rewriting is the process of intercepting incoming Web requests and automatically redirecting requests to other URLs.
For example, if the browser sends a request for Hostname/101. aspx, the server automatically directs the request to http: // hostname/list. aspx? Id = 101.
URL rewriting has the following advantages:
Shorten the URL and hide the actual path to improve security
Easy for users to remember and type.
Easy to be indexed by search engines
II. Basic Method for URL rewriting
Download the MS urlrewriter. dll and put it in the bin of your web application.
1: http://www.rickel.cn/uploads/DevTools/MSDNURLRewriting.msi
2: bytes
After the download is complete, set the following in Web. config:
<? XML version = "1.0" encoding = "UTF-8"?>
<! -- Overred -->
<Configuration>
<Configsections>
<Section name = "rewriterconfig" type = "urlrewriter. config. rewriterconfigserializersectionhandler, urlrewriter"/>
</Configsections>
<Rewriterconfig>
<Rules>
<Rewriterrule>
<Lookfor> ~ /D (\ D +) \. aspx </lookfor>
<Sendto> ~ /Default. aspx? Id = $1 </sendto>
</Rewriterrule>
<Rewriterrule>
<Lookfor> ~ /Default.html </lookfor>
<Sendto> ~ /Default. aspx </sendto>
</Rewriterrule>
</Rules>
</Rewriterconfig>
<System. Web>
<Httphandlers>
<Add verb = "*" Path = "*. aspx" type = "urlrewriter. rewriterfactoryhandler, urlrewriter"/>
<Add verb = "*" Path = "*. html" type = "urlrewriter. rewriterfactoryhandler, urlrewriter"/>
</Httphandlers>
<Compilation DEBUG = "false" strict = "false" Explicit = "true">
<Buildproviders>
<Add extension = ". html" type = "system. Web. Compilation. pagebuildprovider"/>
</Buildproviders>
</Compilation>
</System. Web>
</Configuration>
Note:
Next, you need to add an extension item with the same configuration as the ASPX page during the configuration of your site property home directory in IIS. Note: Do not select "check whether the file exists". Otherwise, the file cannot be found.
Add .html ing to: C: \ WINDOWS \ microsoft.net \ framework \ v2.0.50727 \ aspnet_isapi.dll
How can a pseudo-static page and a static page coexist ???
This is required:
<Compilation DEBUG = "false" strict = "false" Explicit = "true">
<Buildproviders>
<Add extension = ". html" type = "system. Web. Compilation. pagebuildprovider"/>
</Buildproviders>
</Compilation>
Where
<Section name = "rewriterconfig" type = "urlrewriter. config. rewriterconfigserializersectionhandler, urlrewriter"/>
The handler class used to specify the configuration section "rewriterconfig" is named "urlrewriter. config. rewriterconfigserializersectionhandler", which exists in the urlrewriter. dll file in the bin directory.
The key is the two sentences.
<Lookfor> ~ /D (\ D +) \. aspx </lookfor>
<Sendto> ~ /Default. aspx? Id = $1 </sendto>
<Lookfor> ~ /D (\ D +) \. aspx </lookfor>
Indicates the URL entered by the user, D (\ D + )\. aspx is a regular expression that matches the file name in the URL (it starts with the letter D, followed by one or more numbers, and starts. the end of Aspx. You can also set it based on your own needs ).
<Sendto> ~ /Default. aspx? Id = $1 </sendto>
Indicates how to rewrite the URL after the server receives a request that meets the preceding conditions. Here, defalutl. aspx is accessed and the parameter ID is passed in. The value $1 is represented by the first number in the file name of the user request.
For example, if you enter Hostname/d11.aspx, the server will rewrite it to http: // hostname/default. aspx? Id = 11. In other words, when the user inputs http: // hostname/d11.aspx, the actual access is http: // hostname/default. aspx? Id = 11. In this way, the real file name is hidden and easy to remember.
Process sending back
If a sender is generated in the rewritten URL, for example, a button and the overwritten aspx is called, the actual address of the aspx file will be displayed in the user's browser, that is, http: // hostname/default. aspx? Id = 11. But from the user's point of view, if you suddenly see the URL change when you click the button, it will make them feel uneasy. Therefore, this problem must be solved.
There are two solutions:
(1) define an actionlessform class by yourself and no longer use the form tag provided by the system in aspx
Namespace actionlessform
{
Public class form: system. Web. UI. htmlcontrols. htmlform
{
Protected override void renderattributes (htmltextwriter writer)
{
Writer. writeattribute ("name", this. Name );
Base. Attributes. Remove ("name ");
Writer. writeattribute ("method", this. method );
Base. Attributes. Remove ("method ");
This. Attributes. Render (writer );
Base. Attributes. Remove ("action ");
If (base. ID! = NULL)
Writer. writeattribute ("ID", base. clientid );
}
}
}
After creating and compiling this class, you must first add it to the references folder of the Web application to use it in the ASP. NET web application. Then, use it to replace the htmlform class by adding the following content at the top of the ASP. NET webpage:
<% @ Register tagprefix = "SKM" namespace = "actionlessform" assembly = "actionlessform" %>
Then, replace <form runat = "server"> (if any) with: <SKM: Form ID = "form1" method = "Post" runat = "server">
Replace the </form> mark on the right with: </SKM: Form>
This method is not recommended for individuals.
(2) The second method is to inherit the page, so you do not need to modify anything in the ASPX page.
Code:
Using system;
Using system. IO;
Using system. Web;
Using system. Web. UI;
Namespace URL
{
Public class olpage: Page
{
Public olpage ()
{}
Protected override void render (htmltextwriter writer)
{
If (writer is system. Web. UI. html32textwriter)
{
Writer = new formfixerhtml32textwriter (writer. innerwriter );
}
Else
{
Writer = new formfixerhtmltextwriter (writer. innerwriter );
}
Base. Render (writer );
}
}
Internal class formfixerhtml32textwriter: system. Web. UI. html32textwriter
{
Private string _ URL; // a false URL
Internal formfixerhtml32textwriter (textwriter writer): Base (writer)
{
_ Url = httpcontext. Current. Request. rawurl;
}
Public override void writeattribute (string name, string value, bool encode)
{
If (_ URL! = NULL & string. Compare (name, "action", true) = 0)
{
Value = _ URL;
}
Base. writeattribute (name, value, encode );
}
}
Internal class formfixerhtmltextwriter: system. Web. UI. htmltextwriter
{
Private string _ URL;
Internal formfixerhtmltextwriter (textwriter writer): Base (writer)
{
_ Url = httpcontext. Current. Request. rawurl;
}
Public override void writeattribute (string name, string value, bool encode)
{
If (_ URL! = NULL & string. Compare (name, "action", true) = 0)
{
Value = _ URL;
}
Base. writeattribute (name, value, encode );
}
}
}
Compile the file into a DLL and reference it in your project.
Then, rewrite the code of the inherited page class in the CS file corresponding to all aspx files in the project to inherit the olpage.
For example
Public class webform1: Page
Rewrite
Public class webform1: URL. olpage
In this way, the problem of sending back is solved.