ASP. NET pseudo-static UrlRewrite (Url rewriting) Implementation and Configuration
Core tips:On the Internet, you will often see that the address suffixes of many websites use XX. HTML or XX. ASPX and other operations are similar to static file identifiers. have you ever wondered whether it is actually a static file generated one by one? What are the advantages and disadvantages of static file generation, for websites with a large access volume, using static files can improve the stability and speed of server operation...
On the Internet, you will often see that the address suffixes of many websites use XX. HTML or XX. ASPX and other operations are similar to static file identifiers. have you ever wondered whether it is actually a static file generated one by one? What are the advantages and disadvantages of static file generation, for websites with a large volume of traffic, using static files can improve the stability and speed of the server. The disadvantage is that when a list is generated, a record is added and all related lists need to be reconstructed, of course, it is not ruled out that some companies have overcome this problem, but it is still a problem for mass programmers, and the generated version is not necessarily more dynamic than the small and medium websites, this is because management is troublesome.
Structure of the above facts, followed by pseudo-static technology, the standard is URL rewriting technology, he has the best support for the ASPX format, of course, can also rewrite HTML files, however, this requires the setting of IIS, which is more convenient than the ASPX format. This site uses the URL rewriting technology. This technology has obvious advantages: it is convenient for search engines to include and view, and so on. Then let's take a look at how to configure him.
First, you need to reference two DLL files in your project:
URLRewriter. dll
ActionlessForm. dll
The actual rewrite is URLRewriter. dll. But if you want to implement paging, you must use this ActionlessForm. dll
URLRewriter. dll 1. Download an object1: http://download.csdn.net/source/524514
2: http://www.nmju.net/UserFiles/URLRewriter.rar
3: http://download.microsoft.com/download/0/4/6/0463611e-a3f9-490d-a08c-877a83b797cf/MSDNURLRewriting.msi
4: Download from this site
2. Configure the WEB. CONFIG fileReference this Assembly in the project. The pseudo-static syntax is... (for example, the. aspx page is represented by .html. In the web. config file --> Add the following node <configuration>
<ConfigSections>
<Section name = "RewriterConfig" requirePermission = "false" type = "URLRewriter. Config. RewriterConfigSerializerSectionHandler, URLRewriter"/>
</ConfigSections>
<RewriterConfig>
<Rules>
<RewriterRule> <! -- A parameter->
<LookFor> ~ /Cn/ss _ (. [\ d] *) \. html </LookFor>
<SendTo> ~ /Cn/news. aspx? Id = $1 </SendTo>
</RewriterRule>
<RewriterRule> <! -- Transfer two parameters -->
<LookFor> ~ /Cn/ss _ (. [\ d] *) 0 (. [\ w] * ).html </LookFor> <! --(. [\ D] *) (. [\ w] * ).html or ss _,(. [0-9] *), (. [0-9] *) \. html -->
<SendTo> <! [CDATA [~ /Cn/news. aspx? Id = $1 & sid = $2]> </SendTo>
</RewriterRule>
<RewriterRule> <! -- Three parameters -->
<LookFor> ~ /Cn/ss _ (. [\ d] *) 0 (. [\ w] *) 0 (. [\ w] * ).html </LookFor>
<SendTo> <! [CDATA [~ /ZH-CN/news. aspx? Id = $1 & sid = $2 & n_id = $3]> </SendTo>
</RewriterRule>
<RewriterRule>
<LookFor> ~ /Dfsaddf/index (\ d *) \. html </LookFor>
<SendTo> ~ /Favorites_Page.aspx </SendTo>
</RewriterRule>
</Rules>
</RewriterConfig>
</Configuration>
<System. web>
<HttpHandlers>
<Add verb = "*" path = "*. aspx" type = "URLRewriter. RewriterFactoryHandler, URLRewriter"/>
<Add verb = "*" path = "*. html" type = "URLRewriter. RewriterFactoryHandler, URLRewriter"/>
</HttpHandlers>
</System. web>
<! --
<HttpModules>
<Add type = "URLRewriter. ModuleRewriter, URLRewriter" name = "ModuleRewriter"/>
</HttpModules>
-->
After setting the preceding steps, you can basically run it. The <RewriterRule/> label contains a rewritten module, and the <LookFor/> label contains the rewritten address, <SendTo/> is the original address.
3. Configure the iis .html File Configure IIS6.0
IIS configuration: website-> properties-> virtual directory-> Configuration (G)...-> ing-> wildcard application ing-> Add
Executable File: c: \ windows \ microsoft.net \ framework \ v2.0.50727 \ aspnet_isapi.dll
Extension:. html
Check whether the file exists: No.
Configure IIS7.5
Your website --> handler ing in IIS -->
Add script ing (Path: *. html Executable File % windir % \ Microsoft. NET \ Framework \ v2.0.50727 \ aspnet_isapi.dll name: arbitrary, such as Hml)
Add wildcard script ing (Path: * Executable File: C: \ Windows \ Microsoft. NET \ Framework \ v2.0.50727 \ aspnet_isapi.dll name: arbitrary, such as All)
Managed handler ing (Path: * Executable File: System. Web. UI. PageHandlerFactory name, for example, Html-Integrate) -------->
Module in IIS --> Add ----> (Name: any type such as All: URLRewriter. ModuleRewriter hooks OK for sending requests only to the asp.net application or managed handler)
--> OK
4. After the above configuration is complete, you can access it through the following methods:
For example, if you enter hostname/d11.html,
The server will rewrite it to http: // hostname/default. aspx? Id = 11.
In other words, the user enters http: // hostname/d11.html,
Actually accessed is http: // hostname/default. aspx? Id = 11
ActionlessForm. dllNamespace 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" %> replace <form runat = "server"> (if any): <skm: Form id = "Form1" method = "post" runat = "server"> Replace the </form> tag on the right:
</Skm: Form>
Successful!
Virtual Host
We can see on the Internet that many friends use HttpHandle + Server. Transfer to perform urlrewrite in asp.net. In fact, this method is incorrect.
First, HttpHandle cannot implement urlrewrite;
The second Server. Transfer is a standard redirection, and it is not urlrewrite at all.
In fact, you do not need to implement HttpHandle or HttpModule on your own to implement urlrewrite. You can easily implement it with several lines of code. I will introduce that on a VM, the VM is different from its own server. You do not have the permission to modify iis or install iis plug-ins such as iis rewrite. However, we can still easily complete the required functions. The procedure is as follows: open global. asax. cs and locate protected void Application_BeginRequest (Object sender, EventArgs e ). From the method name, I can guess what it is. Enter the following code:
Protected void Application_BeginRequest (Object sender, EventArgs e)
{
String oldUrl = HttpContext. Current. Request. RawUrl;
String pattern = @ "^ (. +) default/(\ d +) \. aspx (\?. *) * $ ";
String replace = "$ 1default. aspx? Id = $2 "; if (Regex. IsMatch (oldUrl, pattern, RegexOptions. IgnoreCase | RegexOptions. Compiled ))
{
String newUrl = Regex. Replace (oldUrl, pattern, replace, RegexOptions. Compiled | RegexOptions. IgnoreCase );
This. Context. RewritePath (newUrl );
}
}
With the above code, I access a code similar :... /default/123. aspx URL, of course, this URL does not exist on my computer, it will be directed :... /default. aspx? Id = 123.
Of course, with a powerful regular expression, you can rewrite the url as needed. All of this is done silently on the server side and won't be noticed on the client side. Because it is on the virtual host, we can only redirect the. aspx file. If it is your own server, you only need to register the Suffix in iis to implement any suffix processing. For example, you can register a type such as *. myweb, so that when someone accesses default/456. myweb, You can redirect it to default. aspx? Id = 456. In a word, as long as you can think of it,. net can help you implement it, and it does not require much code.