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>
  • 相关阅读:
    Ubuntu20 修改网卡名称
    单臂路由实现不同vlan间通信
    配置trunk和access
    基于端口划分vlan
    Zabbix5.0服务端部署
    搭建LAMP环境部署opensns微博网站
    搭建LAMP环境部署Ecshop电商网站
    Zabbix 监控过程详解
    Zabbix agent端 配置
    Zabbix 监控系统部署
  • 原文地址:https://www.cnblogs.com/shinelhui/p/2889588.html
Copyright © 2011-2022 走看看