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重写无后缀的代码请关注haodaima.com其它相关文章!

标签: asax Global