zoukankan      html  css  js  c++  java
  • URL的重写

    (简单)方法一:

    1.首先在网站的根目录下添加一个Global.asax,全局的应用程序
    2.找到Application_BeginRequest
          1)获取应用程序的路径将其变为相对路径
                 

    string url = Request.AppRelativeCurrentExecutionFilePath;

          2)用正则表达式匹配获得的路径
                  

    Match match = Regex.Match(url, @"~/BookList/BookList_(\d+).aspx");

           3)用Context.RewritePath跳转到对应的程序目录

    if (match.Success)
                {
                    string categoryId=match.Groups[1].Value;
                    Context.RewritePath("/BookList/BookList.aspx?categoryId=" + categoryId);
                }

    方法二:

    第一步:新建一个类,实现IHttpModule接口
    第二步:在Init的方法里边注册context.BeginRequest的事件
    第三步:写注册事件的context_BeginRequest的方法。

    void context_BeginRequest(object sender, EventArgs e)
            {
                HttpApplication app = sender as HttpApplication;
                string url = app.Request.RawUrl;//请求的路径
                Regex regex = new Regex(@"index\-(\d+)\.html");
                Match match = regex.Match(url);
                if (match.Success)
                {
                    int id = int.Parse(match.Groups[1].Value);
                    app.Context.RewritePath("Index.aspx?id="+id);
                }
            }

    第四步:在Web.config里边配置。

    <system.web>
          <httpModules>
            <add name="urlrewrite" type="WebUrlRewrite.UrlRewrite"/>//命名控件加类名
          </httpModules>
    <system.web>
  • 相关阅读:
    树莓派4B
    SpringBoot 自定义 info Actuator 原理
    RestTemplate翻译serviceId过程
    ISA, ABI, API区别
    01.编译器结构
    【Git123】Git SSH Key支持多账号
    【消息中间件123】Solace PubSub+ Event Broker介绍
    【ETL123】
    【Http123】Http Timeout
    【性能123】Linux性能之“平均负载”
  • 原文地址:https://www.cnblogs.com/shinelhui/p/2889588.html
Copyright © 2011-2022 走看看