asp.net 页面url重写

不更改情况下,页面路径为index.aspx?id=1,现在输入页面路径index/1时,也能访问到页面,这一过程叫做url重写
①:在一个类里制定路径重写规则,以下为自定义UrlRewriterFilter类,需要继承接口IHttpModule
②:在配置文件里面configuration节点里进行配置(如果自定义类是在另一个类库里面写的,则需要将该类库的.dll文件生成路径改为和启动项.dll文件路径一致)
 
另外,asp.net的url重写还可以直接在网站发布的时候,进行IIS  选择url重写功能进行配置
 
第一步:
using System;
using System .Collections . Generic;
using System .Linq;
using System .Text;
using System .Threading . Tasks;

namespace HttpModule
{
    using System. Web;
    using System. Text .RegularExpressions;
    public class UrlRewriterFilter :IHttpModule
    {
        public void Dispose()
        {
            throw new NotImplementedException ();
        }

        /// <summary>
        /// 负责在第一个管道事件上注册一个重写 index/1的url为 index.aspx?id=1
        /// </summary>
        /// <param name= "context" ></param>
        public void Init( HttpApplication context)
        {
            context . BeginRequest+= context_BeginRequest;
        }

        void context_BeginRequest( object sender, EventArgs e)
        {
            //01.获取当前请求的原始url  index/1
            string url = HttpContext. Current .Request . RawUrl;
            //02.将当前url重写
            // 定义一个正则表达式来检查当前发送过来的url 是否为我要重写的index页面路径
            Regex reg = new Regex ("/index/(.*)" );
            if (reg. IsMatch(url))
            {
                string newUrl = reg .Replace(url, "/index.aspx?id=$1" );
                HttpContext .Current . RewritePath(newUrl);
            }
        }
    }
}

第二步:

  < system.webServer >
    < modules >
      < add name =" indexUrlRewrite " type =" HttpModule.UrlRewriterFilter "/>
    </ modules >
  </ system.webServer >

 

转载于:https://www.cnblogs.com/miaoying/p/5384478.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值