一个URL重写的例子

打开 Global.asax.cs 在最上面添加一行 using System.Text.RegularExpressions;

然后在 Application_BeginRequest 事件中写入

None.gif string  strRawUrl  =  HttpContext.Current.Request.RawUrl;
None.gif
string  strNewUrl;
None.gif
None.gif
if  (Regex.IsMatch(strRawUrl,  @" default.aspx " , RegexOptions.IgnoreCase))
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifstrNewUrl 
= Regex.Replace(strRawUrl, @"default.aspx"@"showforum.aspx\?fld=1");
InBlock.gif
InBlock.gifHttpContext.Current.RewritePath( strNewUrl );
ExpandedBlockEnd.gif}

None.gif
else   if  (Regex.IsMatch(strRawUrl,  @" (\d+)0(\d+)0(\d+)0(\d+).html " , RegexOptions.IgnoreCase))
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifstrNewUrl 
= Regex.Replace(strRawUrl, @"(\d+)0(\d+)0(\d+)0(\d+).html"@"showtopic.aspx\?tid=$1&tpg=$2&bpg=$3&fld=$4");
InBlock.gif
InBlock.gifHttpContext.Current.RewritePath( strNewUrl );
ExpandedBlockEnd.gif}

None.gif
else   if  (Regex.IsMatch(strRawUrl,  @" showtopic.aspx\?tid=(\d+)&tpg=(\d+)&bpg=(\d+)&fld=(\d+) " , RegexOptions.IgnoreCase))
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gifstrNewUrl 
= Regex.Replace(strRawUrl, @"showtopic.aspx\?tid=(\d+)&tpg=(\d+)&bpg=(\d+)&fld=(\d+)"@"$10$20$30$4.html");
InBlock.gif
InBlock.gifHttpContext.Current.Response.Redirect( strNewUrl );
ExpandedBlockEnd.gif}

None.gif
None.gif

第一个IF是最最简单的

第二个IF是当IIS把HTML交给ASP.NET时使用的,嘿嘿

第三个IF是为了解决表单提交时IE的地址栏出现真实的URL地址,是第二个IF的逆操作

其中正则表达式里的0是分隔符,因为页数、ID的最小址都不可能是0
用户IE的地址栏中显示的是http://localhost/AdNT/4561230100101.html
但实际上是http://localhost/AdNT/showtopic.aspx?tid=456123&tpg=10&bpg=1&fld=1

转载于:https://www.cnblogs.com/Hedonister/archive/2005/09/04/229835.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值