Concrete implementation of ASP.net URL rewriting

Source: Internet
Author: User
Tags config error code iis regular expression

  This article mainly introduces the concrete implementation of ASP.net URL rewriting, the need for friends can refer to the

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 that the end can use   12345.html   Replace   show.aspx?id=12345 Such address access   can also achieve the Baidu space   http://hi.jb51.net/wu1987116   replacement   http:// Hi.jb51.net/index.aspx? username=wu1987116   features, supports any extension and no extension   program to adjust the part of 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 After installing the bin directory to find the URLRewriter.dll file   ready to start 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 the web.config this step to modify a few places. Note that the location is different   1 add the following code in the Web.config file, note that you want to put it under <configuration>, <appSettings/> <connectionstrings/ > <system.web> above otherwise the error code is as follows:    <configSections>     <section name= " Rewriterconfig "Type=" URLRewriter.Config.RewriterConfigSerializerSectionHandler, Urlrewriter "/>   </ configsections>     which   Code as follows: <section name= "Rewriterconfig"   type= " URLRewriter.Config.RewriterConfigSerializerSectionHandler, Urlrewriter "/>     used to specify configuration section" Rewriterconfig " The handler class name is "URLRewriter.Config.RewriterConfigSerializerSectionHandler", which exists in the URLRewriter.dll file in the bin directory   2 Under the system.web node in the Web.config file, add the following code   code as follows: <httpHandlers>       <add verb= "*" Path= "*.html "            type=" Urlrewriter.rewriterfactoryhandler, Urlrewriter "/>     &NB Sp <add verb= "*" path= "*"             type= "Urlrewriter.rewriterfactoryhandler, Urlrewriter "/>    </httpHandlers>    " This code means to have the file name extension. html and any extensions (including no extensions, excluding *.html, Because all HTTP requests for the file that this position will process 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 not valid. The following steps have the settings mapped to that page   3 rewrite URL           and 1, which is also placed under the <configuration> node under     &NBS P   The key isThe code is as follows: <RewriterConfig>     <Rules>       <RewriterRule>       &N Bsp <lookfor>~/(. +) .html</lookfor>         <sendto>~/shownews.aspx? showid=$1</sendto>       </RewriterRule>       <RewriterRule>   & nbsp     <lookfor>~/(. +) </LookFor>         <sendto>~/blog.aspx? username=$1</sendto>       </RewriterRule>     </Rules>   </ rewriterconfig>     Effect: When you visit http://127.0.0.1/123.html, are you actually accessing http://127.0.0.1/Shownews.aspx? showid=123           access to http://127.0.0.1/any character, the actual access is http://127.0.0.1/blog.aspx? Username= any character           Note the mapping order in the 2nd, 3 steps   which key in the URL of the conversion     copy code code as follows: <LookFor> ~/(. +) .html</lookfor>   <sendto>~/shownews.aspx? showid=$1</sendto>       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:   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 title </a>   The URL rewrite can be easily implemented by completing the three steps above, but note that if you publish the site, you will find that your URL rewrite is likely to fail,If it fails, you need to set up IIS: Open IIS, the home directory-〉 Configure-〉 mapping-〉 Click the "Insert" wildcard application mapping-〉 select "C:windowsmicrosoft.netframeworkv2.0.50727aspnet_" Isapi.dll ", then remove the tick (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.