以前在做asp的时候,要把 /default.asp?id=123映射成/default/123,需要借助IISRewriter这个组件,到了asp.net以后,可以用代码写了,但是个人觉得很麻烦,要写一堆代码,还要修改web.config,现在好了:asp.net4.0中 asp.net mvc中的路由规则全部可以用于webform了
使用步骤:
1.Global.ascx.cs中先注册路由规则
- using System;
- using System.Web.Routing;
- namespace WebApp
- {
- public class Global : System.Web.HttpApplication
- {
- void RegisterRouters(RouteCollection routes)
- {
- //参数含义:
- //第一个参数:路由名称--随便自己起
- //第二个参数:路由规则
- //第三个参数:该路由规则交给哪一个页面来处理
- routes.MapPageRoute("my-route-name", "default/{id}", "~/default.aspx");
- //...当然,您还可以添加更多路由规则
- }
- protected void Application_Start(object sender, EventArgs e)
- {
- RegisterRouters(RouteTable.Routes);
- }
- }
- }
using System;
using System.Web.Routing;
namespace WebApp
{
public class Global : System.Web.HttpApplication
{
void RegisterRouters(RouteCollection routes)
{
//参数含义:
//第一个参数:路由名称--随便自己起
//第二个参数:路由规则
//第三个参数:该路由规则交给哪一个页面来处理
routes.MapPageRoute("my-route-name", "default/{id}", "~/default.aspx");
//...当然,您还可以添加更多路由规则
}
protected void Application_Start(object sender, EventArgs e)
{
RegisterRouters(RouteTable.Routes);
}
}
}
在根目录下弄个default.aspx来测试下:
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApp.Default" EnableViewState="false" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- <title></title>
- </head>
- <body>
- <form id="form1" runat="server">
- 待提交的Id:<asp:TextBox runat="server" ID="txtId" /><br />
- 接收到的Id:<asp:Label ID="lblId" runat="server" Text="" /><br />
- 其它常规参数:<asp:Label ID="lblOther" Text="" runat="server" /><br />
- <asp:Button Text="提交" runat="server" ID="btn1" />
- </form>
- </body>
- </html>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApp.Default" EnableViewState="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
待提交的Id:<asp:TextBox runat="server" ID="txtId" /><br />
接收到的Id:<asp:Label ID="lblId" runat="server" Text="" /><br />
其它常规参数:<asp:Label ID="lblOther" Text="" runat="server" /><br />
<asp:Button Text="提交" runat="server" ID="btn1" />
</form>
</body>
</html>
后端代码:
- using System;
- using System.Web.UI;
- namespace WebApp
- {
- public partial class Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- string _id = Page.RouteData.Values["id"] as string;//接收路由参数
- lblId.Text = _id;
- string _t = "";
- foreach (var item in Request.QueryString) //如果是用 /default/123?name=xxx之类的传过来的,测试一下能不能收到其它参数
- {
- _t += item + "=" + Request.QueryString[item.ToString()] + ",";
- }
- lblOther.Text = _t.Trim(',');
- }
- else //提交以后的测试
- {
- //看看能不能继续用以前的Request.Form来处理
- string _t = "";
- foreach (var item in Request.Form)
- {
- _t += item + "=" + Request.Form[item.ToString()] + ",";
- }
- lblOther.Text = _t.Trim(',');
- }
- }
- }
- }
using System;
using System.Web.UI;
namespace WebApp
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string _id = Page.RouteData.Values["id"] as string;//接收路由参数
lblId.Text = _id;
string _t = "";
foreach (var item in Request.QueryString) //如果是用 /default/123?name=xxx之类的传过来的,测试一下能不能收到其它参数
{
_t += item + "=" + Request.QueryString[item.ToString()] + ",";
}
lblOther.Text = _t.Trim(',');
}
else //提交以后的测试
{
//看看能不能继续用以前的Request.Form来处理
string _t = "";
foreach (var item in Request.Form)
{
_t += item + "=" + Request.Form[item.ToString()] + ",";
}
lblOther.Text = _t.Trim(',');
}
}
}
}
先看看传统的url参数方式还能不能用(结果证明:加了路由后,以前的方式仍然可运行)
再用路由规则访问试下,同时加了路由中未定义的url参数,看看能不能同时处理,结果再次证明,一切Ok,注意:如果这时又传了?id=xxx参数,仍然可以正常区分出来(见下图)
把路径换成大写试下,结果证明不区分大小写(这一点个人觉得比ror要好)
试下省略掉前面的default.aspx能不能正常默认交给default.aspx来处理,结果ok
试下提交的场景,结果证明,仍然可以用以前的Request.Form来处理,同时源代码查看下来,系统自动把action设置成了路由参数
前台页面写链接时,以前的<a href="/default.aspx?id=123">xxx</a>当然可以继续用,不过从SEO角度考虑,也应该换成<a href="/default/123">xxx</a>了,当然这样并不是最好办法,比如你以后路由规则换了,这个链接就失效了,建议写成:
<a href="<%=this.GetRouteUrl("my-route-name", new { id="123"}) %>">xxx</a>
出处: http://yjmyzz.cnblogs.com