华域联盟 .Net 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>

您可能感兴趣的文章:

  • asp.net 在global中拦截404错误的实现方法
  • Global.cs中自动获取未处理的异常
  • 在Global.asax文件里实现通用防SQL注入漏洞程序(适应于post/get请求)
  • Global.asax取绝对路径的方法
  • Global.asax取物理路径/取绝对路径具体方法
  • Global.asax的Application_Error实现错误记录/错误日志的代码
  • c#定时器和global实现自动job示例
  • ASP.net全局程序文件Global.asax用法分析
  • ASP.NET中Global和URLReWrite用法
  • 在C#中global关键字的作用及其用法

本文由 华域联盟 原创撰写:华域联盟 » Global.asax的Application_BeginRequest实现url重写无后缀的代码

转载请保留出处和原文链接:https://www.cnhackhy.com/51604.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部