Use URLRewriter. dll to implement ASP. NET to implement pseudo-static, asp.net to implement pseudo-static

Source: Internet
Author: User
Tags asp net

Use URLRewriter. dll to implement ASP. NET to implement pseudo-static, asp.net to implement pseudo-static

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, we need to use the URL rewriting component: URLRewriter.rar

Second, you need to make some settings in WEB. CONFIG:

1. Add the following nodes in <system. Web> and </system. web> of web. Config:

<HttpHandlers>
<Add verb = "*" path = "*. aspx" type = "URLRewriter. RewriterFactoryHandler, URLRewriter"/>
<Add verb = "*" path = "*. html" type = "URLRewriter. RewriterFactoryHandler, URLRewriter"/>
</HttpHandlers>

2. Add the following nodes in <configuration> and </configuration> of Web. Config:

<ConfigSections>
<Section name = "RewriterConfig" type = "URLRewriter. Config. RewriterConfigSerializerSectionHandler, URLRewriter"/>
</ConfigSections> <RewriterConfig>
<Rules>
<RewriterRule>
<LookFor> ~ /D (\ d +) \. html </LookFor>
<SendTo> ~ /Default. aspx? MyID = $1 </SendTo>
</RewriterRule>
</Rules>
</RewriterConfig>

After setting the preceding two 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
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.

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 and actually accesses http: // hostname/default. aspx? Id = 11

 

 

5. The following methods must be referenced for paging:

ActionlessForm. dll

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" %> 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.

 

  • URLRewriter.rar (3.8 KB)

Asp net uses UrlRewriterdll for pseudo-static page Rewriting

1. Download The Intelligencia. UrlRewriter. dll file urlrewriter.net

2. Add reference

3. Set the Web. Config configuration file

<? Xml version = "1.0" encoding = "UTF-8"?>

<Configuration>

<ConfigSections>
<Section name = "rewriter" requirePermission = "false" type = "Intelligencia. UrlRewriter. Configuration. RewriterConfigurationSectionHandler, Intelligencia. UrlRewriter"/>
</ConfigSections>

<Rewriter>
<Rewrite url = "~ /(.20.20..html $ "to = "~ /Default. aspx? ID = $1 "/> // place $1 in brackets
</Rewriter>

<System. web>
<HttpModules>
<Add type = "Intelligencia. UrlRewriter. RewriterHttpModule, Intelligencia. UrlRewriter" name = "UrlRewriter"/>
</HttpModules>
</System. web>

</Configuration>

4. Create two pages (Default. aspx, UrlRewtite. aspx)

Default. aspx Interface

Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
Response. Write ("this is a rewritten ~~~~~~~~~~ ");
}
}

UrlRewtite. aspx Interface

<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "TestUrlRewrite. aspx. cs" Inherits = "WebPractise. TestUrlRewrite" %>

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "...... the remaining full text>
 
The pseudo-static problem of asp net using URLRewriterdll

The actiontrail of a table is original. In the form of aspx, the actiontrail is n_32.html.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.