zoukankan      html  css  js  c++  java
  • Global.asax的Application_BeginRequest实现url重写无后缀的代码

    本文为大家详细介绍下利用Global.asax的Application_BeginRequest 实现url重写其无后缀,具体核心代码如下,有需求的朋友可以参考下,希望对大家有所帮助

    利用Global.asax的Application_BeginRequest 实现url 重写 无后缀

    代码如下:

    <%@ Application Language="C#" %>
    
    <script RunAt="server">
    void Application_BeginRequest(object sender, EventArgs e)
    {
    string oldUrl = System.Web.HttpContext.Current.Request.RawUrl; //获取初始url
    
    //~/123.aspx → ~/Index.aspx?id=123
    Regex reg = new Regex(@"^/d+.html");
    if (reg.IsMatch(oldUrl))
    {
    string id = reg.Match(oldUrl).ToString().Substring(1, reg.Match(oldUrl).ToString().LastIndexOf(".") - 1);
    Context.RewritePath("~/Index.aspx?id=" + id);
    }
    
    //~/123 → ~/Index.aspx?id=123
    Regex reg1 = new Regex(@"^/d+$");
    if (reg1.IsMatch(oldUrl))
    {
    string id = reg1.Match(oldUrl).ToString().Substring(1);
    Context.RewritePath("~/Index.aspx?id=" + id);
    }
    
    //~/index/123 → ~/Index.aspx?id=123
    Regex reg3 = new Regex(@"^/index/d+$");
    if (reg3.IsMatch(oldUrl))
    {
    string id = reg3.Match(oldUrl).ToString().Substring(7);
    Context.RewritePath("~/Index.aspx?id=" + id);
    }
    }
    
    </script> 
  • 相关阅读:
    细说进程、应用程序域与上下文之间的关系
    sql server2008添加登录账户配置权限 && 登录时18456错误
    Sql CLR
    设计模式学习篇(一)
    常用实用方法
    OOP 6大基本原则
    如何成为一个优秀的程序员
    家庭网络
    反射
    ADO.Net 事务操作
  • 原文地址:https://www.cnblogs.com/lxshanye/p/4045317.html
Copyright © 2011-2022 走看看