您的位置:首页 > 产品设计 > UI/UE

Global.asax的Application_BeginRequest实现url重写无后缀的代码

2018-10-12 13:57 651 查看
利用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>

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: