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
ASP.NET用戶注冊實戰(zhàn)(第11節(jié))
這篇文章主要介紹了ASP.NET用戶注冊實戰(zhàn),鞏固前10小節(jié)所學的全部知識,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2015-08-08
Microsoft Visual Studio 2017 for Mac Preview安裝使用案例分享
這篇文章主要為大家分享了Microsoft Visual Studio 2017 for Mac Preview安裝使用案例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11
AspNetCore&MassTransit?Courier實現分布式事務的詳細過程
MassTransit?Courier是一種用于創(chuàng)建和執(zhí)行帶有故障補償的分布式事務的機制,它可以用于滿足本地事務的需求,也可以在分布式系統中實現分布式事務,這篇文章主要介紹了AspNetCore&MassTransit?Courier實現分布式事務,需要的朋友可以參考下2022-10-10
在 .NET Framework 2.0 中未處理的異常導致基于 ASP.NET 的應用程序意外退出
如果在 Microsoft .NET Framework 2.0 上構建的基于 Microsoft ASP.NET 的應用程序中引發(fā)未處理的異常,該應用程序將會意外退出。如果出現這個問題,不會在應用程序日志中記錄了解此問題所必需的異常信息。2009-11-11
Visual Studio 2017 15.5 正式發(fā)布!性能再提升
Visual Studio 2017 15.5 正式發(fā)布!性能再提升,時發(fā)布的還有 Visual Studio for Mac 7.3,亮點如下2017-12-12

