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>
</Rules>
</RewriterConfig>
<System. web>
<HttpHandlers>
<Add verb = "*" path = "*. aspx" type = "URLRewriter. RewriterFactoryHandler, URLRewriter"/>
</HttpHandlers>
</System. web>
</Configuration>
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 & lt;/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.
Compile *. dll:/t: library name. cs
Trackback: http://tb.blog.csdn.net/TrackBack.aspx? PostId = 1830462
Reference: http://blog.csdn.net/baogreat/archive/2007/10/18/1830462.aspx