使用Web窗体进行ASP.NET路由

我读过ASP.NET Routing … Goodbye URL重写? 和使用路由使用WebForms是很棒的文章,但仅限于简单的,说明性的,“hello world” – 复杂的例子。

是否有人以非平凡的方式使用ASP.NET路由与Web表单? 有什么需要注意的吗? 性能问题? 进一步推荐阅读我应该先看看我自己的实现?

编辑找到这些额外有用的URL:

  • 如何:使用Web窗体路由(MSDN)
  • ASP.NET路由(MSDN)
  • 如何:从路由构建URL(MSDN)

不确定这是否是你的答案,但这可能会让你朝着正确的方向前进,Scott Hanselman(MSFT)展示如何获得ASP.NET WebForms,ASP.NET MVC和ASP.NET动态数据 – 哦和AJAX一起工作和谐。

http://www.hanselman.com/blog/PlugInHybridsASPNETWebFormsAndASPMVCAndASPNETDynamicDataSideBySide.aspx

.net 4.0和ASP.net路由的两个非常有用的链接

  • 演练:在Web窗体应用程序中使用ASP.NET路由

  • ASP.net路由

前几天我看到这个播客链接到ScottGu的博客,可能对你有用

http://morewally.com/cs/blogs/wallym/archive/2008/10/08/asp-net-podcast-show-125-routing-with-webforms.aspx

Mike Ormond使用ASP.NET设置URL路由的分步指南非常出色(使ASP.NET路由启动并运行 – 权威指南 )

您可以在以下文章中以简单的方式找到URL Routing。 它提供信息,例如,在路由上发送请求,在目标页面上检索URL参数,设置参数的默认值。

ASP.Net Web窗体中的URL路由部分 – 1

ASP.Net Web窗体中的URL路由部分 – 2

一个如何在ASP.NET中使用路由的简单示例

  1. 创建空Web应用程序
  2. 添加第一个表单 – Default.aspx
  3. 添加第二个表单 – Second.aspx
  4. 添加第三个表单 – Third.aspx
  5. 添加到default.aspx 3按钮 –

    protected void Button1_Click(object sender, EventArgs e) { Response.Redirect("Second.aspx"); } protected void Button2_Click(object sender, EventArgs e) { Response.Redirect("Third.aspx?Name=Pants"); } protected void Button3_Click(object sender, EventArgs e) { Response.Redirect("Third.aspx?Name=Shoes"); } 
  6. 在第三页上读取查询字符串

     protected void Page_Load(object sender, EventArgs e) { Response.Write(Request.QueryString["Name"]); } 

现在,如果您运行该程序,您将能够导航到第二和第三个表单。 这就是以前的样子。 我们来添加路由。

  1. 使用System.Web.Routing添加新项目Global.aspx;

     protected void Application_Start(object sender, EventArgs e) { RegisterRoutes(RouteTable.Routes); } void RegisterRoutes(RouteCollection routes) { routes.MapPageRoute( "HomeRoute", "Home", "~/Default.aspx" ); routes.MapPageRoute( "SecondRoute", "Second", "~/Second.aspx" ); routes.MapPageRoute( "ThirdRoute", "Third/{Name}", "~/Third.aspx" ); } 
  2. 在default.aspx中修改protected void Button1_Click(object sender,EventArgs e){// Response.Redirect(“Second.aspx”); Response.Redirect(GetRouteUrl(“SecondRoute”,null)); }

     protected void Button2_Click(object sender, EventArgs e) { //Response.Redirect("Third.aspx?Name=Pants"); Response.Redirect(GetRouteUrl("ThirdRoute", new {Name = "Pants"})); } protected void Button3_Click(object sender, EventArgs e) { // Response.Redirect("Third.aspx?Name=Shoes"); Response.Redirect(GetRouteUrl("ThirdRoute", new { Name = "Shoes" })); } 
  3. 修改third.aspx中的页面加载

     protected void Page_Load(object sender, EventArgs e) { //Response.Write(Request.QueryString["Name"]); Response.Write(RouteData.Values["Name"]); } 

运行程序,请注意url看起来更干净 – 其中没有文件扩展名(Second.aspx变为第二个)

  1. 传递多个参数

    • 使用以下代码将新按钮添加到default.aspx:

       protected void Button4_Click(object sender, EventArgs e) { Response.Redirect(GetRouteUrl("FourthRoute", new { Name = "Shoes" , Gender = "Male"})); } 
    • 将以下代码添加到global.asax

        routes.MapPageRoute( "FourthRoute", "Fourth/{Name}-{Gender}", "~/Fourth.aspx" ); 
    • 使用以下页面加载创建Fourth.aspx页面:

       protected void Page_Load(object sender, EventArgs e) { Response.Write("Name is: " + RouteData.Values["Name"] + " and Gender is " + RouteData.Values["Gender"]); }