Global.asax的Application_BeginRequest实现url重写无后缀的代码
更新时间:2013年08月14日 17:27:11 作者:
本文为大家详细介绍下利用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>
复制代码 代码如下:
<%@ 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>
相关文章
IIS Express 取代 ASP.NET Development
这篇文章主要介绍了IIS Express 取代 ASP.NET Development Server的配置方法,需要的朋友可以参考下2023-06-06
Microsoft Visual Studio 2017 for Mac Preview安装使用案例分享
这篇文章主要为大家分享了Microsoft Visual Studio 2017 for Mac Preview安装使用案例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下2016-11-11
AspNetCore&MassTransit Courier实现分布式事务的详细过程
MassTransit Courier是一种用于创建和执行带有故障补偿的分布式事务的机制,它可以用于满足本地事务的需求,也可以在分布式系统中实现分布式事务,这篇文章主要介绍了AspNetCore&MassTransit Courier实现分布式事务,需要的朋友可以参考下2022-10-10
在 .NET Framework 2.0 中未处理的异常导致基于 ASP.NET 的应用程序意外退出
如果在 Microsoft .NET Framework 2.0 上构建的基于 Microsoft ASP.NET 的应用程序中引发未处理的异常,该应用程序将会意外退出。如果出现这个问题,不会在应用程序日志中记录了解此问题所必需的异常信息。2009-11-11
Visual Studio 2017 15.5 正式发布!性能再提升
Visual Studio 2017 15.5 正式发布!性能再提升,时发布的还有 Visual Studio for Mac 7.3,亮点如下2017-12-12


最新评论