Asp.net does not need to set iis to implement url rewriting similar to pseudo-static Routing

Source: Internet
Author: User

The program has only two parts to adjust.
First, the web. config file.
The second is the link address.
Urlrewrite. dll

First download URLRewriter: http://download.microsoft.com/download/0/4/6/0463611e-a3f9-490d-a08c-877a83b797cf/MSDNURLRewriting.msi

Download and install the URLRewriter. dll file in the bin directory.

Now let's start implementation.
Step 1: Download urlrewrite. dll to your web program directory. Everything works. I put it in bin. Add references to reference urlrewrite. dll.
Step 2: modify web. config
In this step, you need to modify the settings. Note that the location is different.

1. Add the following code to the web. config file. Note that the Code should be placed under <configuration>, <etettings/>

Copy codeThe Code is as follows:
<ConnectionStrings/> <system. web>. Otherwise, an error occurs.

<ConfigSections>
<Section name = "RewriterConfig" type = "URLRewriter. Config. RewriterConfigSerializerSectionHandler, URLRewriter"/>
</ConfigSections>

Where
Copy codeThe Code is as follows:
<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.

2. Add the following code to the system. web node in the web. config file:
Copy codeThe Code is as follows:
<HttpHandlers>
<Add verb = "*" path = "*. html"
Type = "URLRewriter. RewriterFactoryHandler, URLRewriter"/>
<Add verb = "*" path = "*"
Type = "URLRewriter. RewriterFactoryHandler, URLRewriter"/>
</HttpHandlers>

This code refers to the extension of the file named .html and any extension (including no extension, not including *. html, because this location will be processed first) All the HTTP requests of the file are mapped to the URLRewriter class. rewriterFactoryHandler. Pay attention to the sequence and run it from top to bottom. If path = "*" is above, the following html ing is invalid. The following steps have settings mapped to that page.

3. Rewrite the url

Like 1, it is placed under the <configuration> node

The key is

Copy codeThe Code is as follows:
<RewriterConfig>
<Rules>
<RewriterRule>
<LookFor> ~ /(.20.0000.html </LookFor>
<SendTo> ~ /Shownews. aspx? ShowID = $1 </SendTo>
</RewriterRule>
<RewriterRule>
<LookFor> ~ /(. +) </LookFor>
<SendTo> ~ /Blog. aspx? UserName = $1 </SendTo>
</RewriterRule>
</Rules>
</RewriterConfig>

Result: When you access http: // 127.0.0.1/123.html, the actual access is http: // 127.0.0.1/Shownews. aspx? ShowID = 123

When accessing http: // 127.0.0.1/any character, the actual access is http: // 127.0.0.1/blog. aspx? UserName = any character

Note the ing sequence in step 2 and 3.

The key is url conversion.
Copy codeThe Code is as follows:
<LookFor> ~ /(.20.0000.html </LookFor>

<SendTo> ~ /Shownews. aspx? ShowID = $1 </SendTo>

It means converting the first path into another path. The Regular Expression in <LookFor> () is the parameter $1 in the second sentence.

You can also use $2 $3 to represent the parameters in the second third () of <LookFor>.

Multiple parameters:
Copy codeThe Code is as follows:
<ReWriterUrls>
<Rule>
<Old> (. *)/TestUrlRe/file (. *)/(. *) \. html </old>
<New>.../WebForm1.aspx? Id = $2 & type = $3 </new>
</Rule>
<Rule>
<Old> (. *)/TestUrlRe/t (. *)/(. *) \. html </old>
<New>.../WebForm1.aspx? Tid = $2 & ttype = $3 </new>
</Rule>
</ReWriterUrls>

Step 3: write in the page program as follows:
Copy codeThe Code is as follows:
<A href = "news _ <% = newsid %>. html" target = "_ blank"> news Title </a>

After completing the above three steps, you can easily implement URL rewriting. However, note that if you publish a website, you will find that your URL rewriting may fail, if it fails, you need to set IIS:
Open IIS, main directory-> Configuration-> ing-> click "insert" wildcard application ing-> select "C: \ WINDOWS \ Microsoft. NET \ Framework \ v2.0.50727 \ aspnet_isapi.dll ", remove the check box (must be removed), and then confirm.
After the preceding settings are complete, you can view them normally.

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.