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

问题描述:

我读过ASP.NET Routing… Goodbye URL rewriting?Using Routing With WebForms,这些文章都很棒,但仅限于简单的说明性“hello world”复杂性示例。使用Web窗体进行ASP.NET路由

是否有人在那里使用ASP.NET路由与web表单在一个不平凡的方式?任何需要注意的问题?性能问题?在进入我自己的实施之前,我应该看看更多的推荐阅读内容吗?

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

+0

请标记url-routing到您的文章 – Armstrongest 2008-11-11 01:29:11

不知道这是你的答案,但是这可能让你在正确的方向是Scott Hanselman(MSFT)展示了如何获得ASP.NET Web表单,ASP.NET MVC和ASP.NET动态数据 - 哦和AJAX协调一致地工作。

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

我看到这个播客从ScottGu的博客那天挂这可能是对你有用

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

迈克·奥蒙德的一步一步的指导,建立URL与路由ASP.NET非常好(Getting ASP.NET Routing Up and Running - The Definitive Guide

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

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

URL Routing in ASP.Net Web Forms Part - 1

URL Routing in ASP.Net Web Forms Part - 2

如何在ASP.NET

使用路由一个简单的例子
  1. 创建空的Web应用程序
  2. 添加第一种形式 - Default.aspx的
  3. 添加第二种形式 - Second.aspx
  4. 添加第三个表单 - Third.aspx
  5. 添加到默认设置。ASPX 3个按钮 - 第三页

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

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"); 
} 
  • 阅读查询字符串现在,如果你运行程序,你将能够浏览到第二和第三种形式。 这是过去的样子。 让我们添加路由。

    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中修改 保护无效的button1_Click(对象发件人,EventArgs的) { //的Response.Redirect( “Second.aspx”); Response.Redirect(GetRouteUrl(“SecondRoute”,null)); }在third.aspx

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

    运行程序

    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" })); 
    } 
    
  • 修改页面加载,请注意,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"]); 
        }