Global.asax的Application_BeginRequest实现url重写无后缀的代码
程序员文章站
2024-02-28 17:40:28
利用global.asax的application_beginrequest 实现url 重写 无后缀 复制代码 代码如下: <%@ application lang...
利用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>
上一篇: Lesson01_01 HTML基础
推荐阅读