asp.net URL Rewrite specific implementation _ Practical skills

Source: Internet
Author: User

Talking about the need to set up IIS, mainly in order to implement in the virtual host or can not get the IIS operating restrictions, cannot add ISAP and want to implement similar to the static implementation of the program, first of all, the result here to achieve the effect is, eventually can use

12345.html

Replace

show.aspx?id=12345 Such an address access

Baidu can also achieve the space

http://hi.jb51.net/wu1987116

Replace

http://hi.jb51.net/index.aspx?UserName=wu1987116

Features, supports arbitrary extensions and no extensions

The part of the program to adjust is only two pieces.
One is the Web.config file.
The second is the link address.
Required Urlrewrite.dll

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

Download the installation and then find the URLRewriter.dll file in the bin directory

All right, start the implementation.
The first step: Download the Urlrewrite.dll to your Web App directory. It's all right. I put it in the bin. Then add the reference to the Urlrewrite.dll reference.
Step Two: Modify Web.config
This step has to be modified in a few places. Be aware that the position is different

1 Add the following code to the Web.config file, pay attention to <configuration> below, <appSettings/>
<connectionStrings/> <system.web> There's going to be a mistake.

Copy Code code as follows:

<configSections>
<section name= "Rewriterconfig" type= "URLRewriter.Config.RewriterConfigSerializerSectionHandler, Urlrewriter"/ >
</configSections>

which

Copy Code code as follows:

<section name= "Rewriterconfig"
Type= "URLRewriter.Config.RewriterConfigSerializerSectionHandler, Urlrewriter"/>

The name of the handler class used to specify the configuration section "Rewriterconfig" is "URLRewriter.Config.RewriterConfigSerializerSectionHandler", This class 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 Code code as follows:

<add verb= "*" Path= "*.html"
Type= "Urlrewriter.rewriterfactoryhandler, Urlrewriter"/>
<add verb= "*" path= "*"
Type= "Urlrewriter.rewriterfactoryhandler, Urlrewriter"/>

This code means that all HTTP requests for files that have the file name extension. html and any extension (including no extensions, excluding *.html, because this location is processed first) are mapped to class Urlrewriter.rewriterfactoryhandler. Note the order, press from top to bottom, and if path= "*" is above, the following HTML mapping is invalid, and the following steps have the settings mapped to that page

3 overriding URLs

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

The point is

Copy Code code as follows:

<RewriterConfig>
<Rules>
<RewriterRule>
<lookfor>~/(. +) .html</lookfor>
<sendto>~/shownews.aspx? Showid=$1</sendto>
</RewriterRule>
<RewriterRule>
<lookfor>~/(. +) </LookFor>
<sendto>~/blog.aspx? Username=$1</sendto>
</RewriterRule>
</Rules>
</RewriterConfig>

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

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

Note the sequence of mappings in the 2nd, 3 steps

Which key in the conversion of the URL

Copy Code code as follows:

<lookfor>~/(. +) .html</lookfor>

<sendto>~/shownews.aspx? Showid=$1</sendto>

It means to turn the first path into another path. The regular expression in <LookFor> () is the argument in the second sentence.

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

Multiple parameters:

Copy Code code 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 three: You can write this in the page program:
<a href= "news_<%=newsid%>.html" target= "_blank" > news headlines </a>

The URL rewrite is easy to implement with the above three steps, but note that if you publish the site, you will find that your URL rewrite may be invalidated, and if it fails, you need to set up IIS:
Open IIS, home directory-〉 Configure-〉 mapping-〉 Click the "Insert" wildcard application mapping-〉 Select "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll", Then remove the check (must be removed), and then determine.
After the above settings, you can browse the normal.

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.