【问题标题】:ASP.NET Routing with Web Forms带有 Web 窗体的 ASP.NET 路由
【发布时间】:2010-09-15 13:08:58
【问题描述】:

我已经阅读了 ASP.NET Routing… Goodbye URL rewriting?Using Routing With WebForms 的精彩文章,但仅限于简单、说明性的“hello world”复杂示例。

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

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

【问题讨论】:

  • 请将标签 url 路由到您的帖子

标签: c# asp.net url url-routing


【解决方案1】:

一个简单的例子,说明如何在 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. 添加新项目 - Global.aspx 使用 System.Web.Routing;

    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(对象发送者,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 变成了 Second)

  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"]);
      }
      

【讨论】:

  • 有没有办法像MVC那样使用方法属性来设置web方法的路由?
【解决方案2】:

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

URL Routing in ASP.Net Web Forms Part - 1

URL Routing in ASP.Net Web Forms Part - 2

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      Mike Ormond 的使用 ASP.NET 设置 URL 路由的分步指南非常好 (Getting ASP.NET Routing Up and Running - The Definitive Guide )

      【讨论】:

        【解决方案5】:

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

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

        【讨论】:

          【解决方案6】:

          不确定这是否是您的答案,但这可能会让您朝着正确的方向前进和谐共处。

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

          【讨论】:

            猜你喜欢
            • 2013-10-04
            • 2011-03-13
            • 1970-01-01
            • 1970-01-01
            • 2018-07-12
            • 1970-01-01
            • 1970-01-01
            • 2017-11-06
            • 1970-01-01
            相关资源
            最近更新 更多