Global.asax的Application_BeginRequest實現(xiàn)url重寫無后綴的代碼
更新時間:2013年08月14日 17:27:11 作者:
本文為大家詳細(xì)介紹下利用Global.asax的Application_BeginRequest 實現(xiàn)url重寫其無后綴,具體核心代碼如下,有需求的朋友可以參考下,希望對大家有所幫助
利用Global.asax的Application_BeginRequest 實現(xiàn)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>
復(fù)制代碼 代碼如下:
<%@ 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錯誤的實現(xiàn)方法
- Global.cs中自動獲取未處理的異常
- 在Global.asax文件里實現(xiàn)通用防SQL注入漏洞程序(適應(yīng)于post/get請求)
- Global.asax取絕對路徑的方法
- Global.asax取物理路徑/取絕對路徑具體方法
- Global.asax的Application_Error實現(xiàn)錯誤記錄/錯誤日志的代碼
- c#定時器和global實現(xiàn)自動job示例
- ASP.net全局程序文件Global.asax用法分析
- ASP.NET中Global和URLReWrite用法
- 在C#中g(shù)lobal關(guān)鍵字的作用及其用法
相關(guān)文章
C#數(shù)據(jù)綁定控件中的DataSource屬性淺談
使用該屬性指定用來填充Repeater控件的數(shù)據(jù)源。DataSource可以是任何System.Collections.IEnumerable對象, 如用于訪問數(shù)據(jù)庫的System.Data.DataView、System.Collections.ArrayList、System.Collections.Hashtable、數(shù)組或IListSource對象2013-02-02C# 自定義異??偨Y(jié)及嚴(yán)格遵循幾個原則
在C#中所有的異常類型都繼承自System.Exception,也就是說,System.Exception是所有異常類的基類. 總起來說,其派生類分為兩種,需要了解的朋友可以參考下2012-12-12Asp.Net 文件操作基類(讀取,刪除,批量拷貝,刪除,寫入,獲取文件夾大小,文件屬性,遍歷目錄)
Asp.Net 文件操作基類(讀取,刪除,批量拷貝,刪除,寫入,獲取文件夾大小,文件屬性,遍歷目錄),需要的朋友可以參考下2008-07-07淺談ASP.NETCore統(tǒng)一處理404錯誤都有哪些方式
本文主要介紹了ASP.NETCore統(tǒng)一處理404錯誤都有哪些方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04.Net中的Http請求調(diào)用詳解(Post與Get)
在我們服務(wù)端調(diào)用第三方接口時,如:支付寶,微信支付,我們服務(wù)端需要模擬http請求,下面這篇文章主要給大家介紹了關(guān)于.Net中Http請求調(diào)用(Post與Get)的相關(guān)資料,需要的朋友可以參考下2022-09-09