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>
  • 相关阅读:
    Rsync实现文件同步的算法(转载)
    Python模拟登录cnblogs
    负载均衡中四层和七层的介绍(转帖)
    Lvs+Keepalived实现MySQL高可用
    MySQL配置主主及主从备份
    Vim扩展YouCompleteMe插件
    使用Git
    Django回忆录
    Ansible安装配置及使用
    Hive学习之四 《Hive分区表场景案例应用案例,企业日志加载》 详解
  • 原文地址:https://www.cnblogs.com/shinelhui/p/2889588.html
Copyright © 2011-2022 走看看