1. url rewrite
URL rewrite 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 rewrite has the following advantages:
1. Shorten the URL and hide the actual path to improve security
2. easy for users to remember and type
3. Easy to be indexed by search engines
There are many ways to implement URL rewrite. I chose the best one I think. The advantage of this method is that you do not need to change any IIS-related settings, and can also be used in Medium-Trust Security Level virtual hosts.
II. Implementation Details
1. Prepare the urlrewrite. Net class library and reference this class library on the website;
2. Add configsections to the first section of Configuration:
<Configsections>
<SectionName="Rewriter"
Requirepermission="False"
Type="Intelligencia. urlrewriter. configuration. rewriterconfigurationsectionhandler, intelligencia. urlrewriter"/>
</Configsections>
3. Add the processing of httpmodule to system. Web:
<Httpmodules>
<AddName="Urlrewriter"Type="Intelligencia. urlrewriter. rewriterhttpmodule, intelligencia. urlrewriter"/>
</Httpmodules>
4. Add rewrite settings in Configuration:
< Rewriter >
< Rewrite URL = " ~ /Articles/(\ D +) " To = " ~ /Articles/detail. aspx? Id = $1 " />
< Rewrite URL = " ~ /Categories/(\ D +) " To = "~ /Articles/list. aspx? Id = $1 " />
< Rewrite URL = " ~ /Categories/(\ D +) _ (\ D +) " To = " ~ /Articles/list. aspx? Id = $1 & Amp; Page = $2 " />
</ Rewriter >
Note that when multiple parameters are used, HTML Escape characters must be used instead of the & symbol.
Iii. IIS configuration
In IIS 7, urlrewriter. Net can be used without configuration, but in IIS 6, some configuration is required. For details, see the following link.
Http://urlrewriter.net/index.php/support/installation/windows-server-2003
Iv. ASP. NET PostBack settings
After urlrewriter is used, Asp. the PostBack mechanism in. Net causes the original link of the page to be used in the browser after the page is sent back, which is easy to confuse users and is not conducive to search by the search engine. Therefore, it must be processed.
After ASP. NET 2.0, we can use the extension framework of the control adapter to customize and override the action attribute of <form> without havingCodeMake any changes.
1. In app_code, add the formrewriter. CS file:
Using System . Web ;
Using System .Web . UI ;
Public classFormrewritercontroladapter:System.Web.UI.Adapters.Controladapter
{
Protected override voidRender(HtmltextwriterWriter)
{
Base.Render(NewRewriteformhtmltextwriter(Writer));
}
}
public class rewriteformhtmltextwriter : htmltextwriter
{
Public rewriteformhtmltextwriter ( htmltextwriter writer )
:< span style =" color: blue; "> base ( writer )
{< br> innerwriter = writer . innerwriter ;
}
PublicRewriteformhtmltextwriter(System.Io.TextwriterWriter)
:Base(Writer)
{
Innerwriter=Writer;
}
Public override void Writeattribute ( String Name , String Value , Bool Fencode )
{
If (( Name = "Action" ))
{
Httpcontext Context = Httpcontext . Current ;
If ( Context . Items ["Actionalreadywritten" ] = Null )
{
Value = Context . Request . Rawurl ;
Context . Items [ "Actionalreadywritten" ] = True ;
}
}
Base . Writeattribute ( Name , Value , Fencode );
}
}
2. In app_browsers, add the form. Browser file:
< Browsers >
< Browser RefID = " Default ">
< Controladapters >
< Adapter Controltype = " System. Web. UI. htmlcontrols. htmlform " Adaptertype = " Formrewritercontroladapter " />
</ Controladapters >
</ Browser >
</ Browsers >
After this processing, you can ensure that the page still uses the original URL as the link after the PostBack occurs.
From: http://kb.vigal.net/asp-net-url-rewrite/