Application_BeginRequest url重写 Global.asax的Application_BeginRequest实现url重写无后缀的代码

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Application_BeginRequest url重写 Global.asax的Application_BeginRequest实现url重写无后缀的代码

  2021-03-21 我要评论
想了解Global.asax的Application_BeginRequest实现url重写无后缀的代码的相关内容吗,在本文为您仔细讲解Application_BeginRequest url重写的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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>

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们