asp.net 页面url重写

    xiaoxiao2021-04-18  59

    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://ju.6miu.com/read-674836.html

    最新回复(0)