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

发布时间:2022-04-16 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了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>

脚本宝典总结

以上是脚本宝典为你收集整理的Global.asax的Application_BeginRequest实现url重写无后缀的代码全部内容,希望文章能够帮你解决Global.asax的Application_BeginRequest实现url重写无后缀的代码所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:Global.asax