ASP.NET纯代码实现伪静态地址(URL重写)

本人才疏学浅,具体实现原理不敢多说,仅仅写出使用方法,供大家参考.(本例用Http模块重写url)
一实现ShowPlay1.aspx重写到ShowPlay.aspx?vid=1的方法:
1  下载本dll,放到网站跟目录下bin目录下:
http://www.ckcom.cn/urlrewriter.rar

2  在Web.Config的<system.web>和</system.web>添加以下节点:

<httpModules>
   <add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter" />
</httpModules>

3 在Web.Config的<configuration>和</configuration>添加以下节点:

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

<RewriterConfig>
  <Rules>
   <!-- 重写规则-->
   <RewriterRule>
    <!--要实现的url格式-->
    <LookFor>~/ShowPlay-(\d{1,4})\.aspx</LookFor>
    <!--真实的url地址.$1.$2,$3...$N代表正则匹配的第N个表达式-->
    <SendTo>~/ShowPlay.aspx?vid=$1</SendTo>
   </RewriterRule>
  </Rules>
 </RewriterConfig>

4.在网页中添加类似 ShowPlay-1.aspx 的连接 将被重写到 ShowPlay.asp?vid=1


二.实现http://www.ckcom.cn/ShowPlay1/重写到http://www.ckcom.cn/ShowPlay.aspx?vid=1

方法与一的步骤相同,需要额外保证:将Default.aspx添加到网站默认主文档,网站目录下要存在ShowPlay1目录 并且ShowPlay1目录下要有Default.aspx.
相应的重写规则应改为

    <!--要实现的url格式-->
    <LookFor>~/ShowPlay-(\d{1,4})/Default\.aspx</LookFor>
    <!--真实的url地址.$1.$2,$3...$N代表正则匹配的第N个表达式-->
    <SendTo>~/ShowPlay.aspx?vid=$1</SendTo>

用此方法注意:
1.不能使用Windows身份验证用户权限. 应使用Form验证,在web.config配置为:<authentication mode="Forms" />
2.使用Request.ServerVariables["script_name"]获得的路径仍然是:ShowPlay.asp?vid=1
3.被重写的地址如果回发,重写将失效 显示的地址将是ShowPlay.asp?vid=1
4. 后缀名必须为.aspx.如果是其他自定义后缀名,如.net  请在iis将.net映射到aspnet_isapi.dll.这样.net请求才能
到达asp.net引擎.


水平有限 到此即止. 详细的原理和问题解决方法请参考:
http://www.microsoft.com/china/msdn/library/webservices/asp.net/URLRewriting.mspx?pf=true
posted @ 2007-06-14 15:38  waemz  阅读(1565)  评论(0编辑  收藏  举报