• 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 >
  • 相关阅读:
    gflag使用
    INTERVIEW #2
    Decision Tree
    Java FAQ
    K-Nearest Neighbors
    INTERVIEW #1
    C++ FAQ
    INTERVIEW #0
    Selection Sort
    TCP 3-Way Handshake
  • 原文地址:https://www.cnblogs.com/miaoying/p/5384478.html
Copyright © 2020-2023  润新知