ASP.net的URL重写

ASP.net的URL重写


有关于URL的重写,本文也只是拿来主意。相继有MS的组件“URLRewriter”和在Global.asax里的“Application_BeginRequest()”编码方式,以及IIS里的ISAPI设置。

娜列下来,实现方法也都很简单。



方法一:MS组件

这里也不用详解了,相关请看:

http://www.microsoft.com/china/msdn/library/webservices/asp.net/URLRewriting.mspx

用法很简单,只需要把组件URLRewriter.dll拷到应用程序的bin目录下,然后在web.config下加入如下代码:

在<configuration></configuration>中加入:

<configSections>

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

</configSections>



<RewriterConfig>

<Rules>

<RewriterRule>

<LookFor>~/(\d{4})/(\d{2})/Default\.aspx</LookFor>

<SendTo>~/Default.aspx?ID=$1</SendTo>

</RewriterRule>

</Rules>

</RewriterConfig>

然后在<system.web></system.web>中加入:

<httpHandlers>

<add verb="*" path="*.aspx"

type="URLRewriter.RewriterFactoryHandler, URLRewriter" />

</httpHandlers>



最后在地址栏上键入:http://localhost/Test/2004/12/News.aspx

效果出来了。

上面的<LookFor>~/(\d{4})/(\d{2})/News\.aspx</LookFor>这句这正则表达式URL,即被重写的URL,而<SendTo>~/Default.aspx?ID=$1</SendTo>这一句为原始URL地址。其中的$1为第一个正则表达式值(上面例子为:2004),以此类推,第二个即为$2
原文地址:https://www.cnblogs.com/chjf2008/p/1276786.html