zoukankan      html  css  js  c++  java
  • 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 >
  • 相关阅读:
    【分治法】线性时间选择(转)
    【分治法】最接近点对问题(转)
    概率DP入门总结 16题(转)
    动态规划初探及什么是无后效性? (转)
    第15章DP(转)
    整数快速乘法/快速幂+矩阵快速幂+Strassen算法 (转)
    矩阵乘法的理解(转)
    算法导论第4章习题与思考题(转)
    Transaction Script模式
    注册服务
  • 原文地址:https://www.cnblogs.com/miaoying/p/5384478.html
Copyright © 2011-2022 走看看