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

本文介绍如何使用URLRewriter库在ASP.NET中实现URL重写。通过几个步骤即可完成从ShowPlay1.aspx到带有参数的ShowPlay.aspx的重定向,并提供注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本人才疏学浅,具体实现原理不敢多说,仅仅写出使用方法,供大家参考.(本例用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

转载于:https://www.cnblogs.com/waemz/archive/2007/06/14/783645.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值