您的位置:首页 > 移动开发

ASP.NET WebForms MapPageRoute 路由配置

2015-02-26 19:24 429 查看
MapPageRoute 应该是 ASP.NET 4.0 中的东西,但现在我是第一次使用它,使用场景是:MVC 混合使用 WebForm,然后对 WebForm 进行路由配置,当然也可以使用 ISAPI_Rewrite 3,不同的是要在 IIS 中配置,相对而言,MapPageRoute 在程序中进行配置更加灵活方便,下面是关于 MapPageRoute 的简单整理。

1. 最简单:

public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
RouteTable.Routes.MapPageRoute("WebFormRoute",
"test",
"~/WebForm1.aspx");
}
}

public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("hello world!");
}
}

效果:



2. 带参数:

public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
RouteTable.Routes.MapPageRoute("WebFormRoute",
"test/{para}",
"~/WebForm1.aspx");
}
}

public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("hello world! <br />");
Response.Write("para: " + Page.RouteData.Values["para"]);
}
}

效果:



注意:

当我们使用参数映射的时候,在获取参数值使用的是
Page.RouteData.Values
,而不是
Request.QueryString
,有一种情况是,如果你的 Url 映射还是 WebForm1.aspx 文件名的形式,只不过这个文件地址发生了变化,这时候你就不需要在 MapPageRoute 中进行参数配置了,只需要进行 Url 和 WebForm 文件地址配置,那这样配置,使用
Request.QueryString
可以获取到参数值吗?我试过是可以的,没有了参数映射配置,也就不需要
Page.RouteData.Values
进行获取参数值了,一种偷巧行为。

3. 带多个参数:

public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
RouteTable.Routes.MapPageRoute("WebFormRoute",
"test/{para1}/{para2}",
"~/WebForm1.aspx");
}
}

public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("hello world! <br />");
Response.Write("para1: " + Page.RouteData.Values["para1"]+ "<br />");
Response.Write("para2: " + Page.RouteData.Values["para2"]+ "<br />");
}
}

效果:



注意:

多个参数的映射还有一种写法是:
test/{para1}&{para2}
,但我试过这种写法会报错,而在 Scott Guthrie 的一篇博文中评论回复,这种方式是可以的,如下:



4. 非正规参数:

public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
RouteTable.Routes.MapPageRoute("WebFormRoute",
"test/{para1}/{para2}.html",
"~/WebForm1.aspx");
}
}

public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("hello world! <br />");
Response.Write("para1: " + Page.RouteData.Values["para1"]+ "<br />");
Response.Write("para2: " + Page.RouteData.Values["para2"]+ "<br />");
}
}

效果:



注意:

<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>

在 MapPageRoute Url 映射的时候,使用的后缀是 .html,需要在 web.config 添加上面配置,如果使用的是 .aspx,则不需要,runAllManagedModulesForAllRequests 在之前的博文中有说过,就不重复了。

4. MapPageRoute 方法参数:

//参数
public Route MapPageRoute(
string routeName,//路由的名称。
string routeUrl,//路由的 URL 模式。
string physicalFile,//路由的物理 URL。
bool checkPhysicalUrlAccess,//一个值,该值指示 ASP.NET 是否应验证用户是否有权访问物理 URL(始终会检查路由 URL)。
RouteValueDictionary defaults,//路由参数的默认值。
RouteValueDictionary constraints,//一些约束,URL 请求必须满足这些约束才能作为此路由处理。
RouteValueDictionary dataTokens//与路由关联的值,但这些值不用于确定路由是否匹配 URL 模式。
)

public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
RouteTable.Routes.MapPageRoute("WebFormRoute",
"test/{para1}/{para2}/{*queryvalues}",
"~/WebForm1.aspx",
false,
null,
new RouteValueDictionary { { "para1", "^[0-9]*$" }, { "para2", "^[A-Za-z]+$" } },
new RouteValueDictionary { { "para3", "xishuai1" }, { "para4", "xishuai2" } });
}
}

public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("hello world! <br />");
Response.Write("para1: " + Page.RouteData.Values["para1"]+ "<br />");
Response.Write("para2: " + Page.RouteData.Values["para2"]+ "<br />");
Response.Write("para3: " + Page.RouteData.DataTokens["para3"] + "<br />");
Response.Write("para4: " + Page.RouteData.DataTokens["para4"]+ "<br />");
}
}

效果:



参考资料:

URL Routing with ASP.NET 4 Web Forms (VS 2010 and .NET 4.0 Series)

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