Global.asax的Application_BeginRequest實現(xiàn)url重寫無后綴的代碼
更新時間:2013年08月14日 17:27:11 作者:
本文為大家詳細介紹下利用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>
復制代碼 代碼如下:
<%@ 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注入漏洞程序(適應于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#中global關鍵字的作用及其用法
相關文章
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-02Asp.Net 文件操作基類(讀取,刪除,批量拷貝,刪除,寫入,獲取文件夾大小,文件屬性,遍歷目錄)
Asp.Net 文件操作基類(讀取,刪除,批量拷貝,刪除,寫入,獲取文件夾大小,文件屬性,遍歷目錄),需要的朋友可以參考下2008-07-07淺談ASP.NETCore統(tǒng)一處理404錯誤都有哪些方式
本文主要介紹了ASP.NETCore統(tǒng)一處理404錯誤都有哪些方式,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-04-04