首页 文章

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

提问于
浏览
12

我读过ASP.NET 路由...再见 URL 重写?使用 WebForms 路由这些都是很棒的文章,但仅限于简单,说明性的“hello world”-complexity 例子。

是否有人以 non-trivial 方式使用带有 Web 表单的 ASP.NET 路由?任何需要注意的问题?性能问题?进一步推荐阅读我应该先看看我自己的实现?

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

6 回答

  • 3

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

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

  • 3

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

  • 2

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

    • 创建空 Web 应用程序

    • 添加第一个表单 - Default.aspx

    • 添加第二个表单 - Second.aspx

    • 添加第三种形式 - Third.aspx

    • 添加到 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");
    }
    
    • 在第三页上读取查询字符串
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write(Request.QueryString["Name"]);
    }
    

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

    • 使用 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"
        );
    }
    
    • 在 default.aspx modify 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" }));
    }
    
    • 在 third.aspx 中修改页面加载
    protected void Page_Load(object sender, EventArgs e)
    {
        //Response.Write(Request.QueryString["Name"]);
        Response.Write(RouteData.Values["Name"]);
    }
    

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

    • 传递多个参数

    • 使用以下代码向 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"]);
    }
    
  • 1

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

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

  • 0

    Mike Ormond 使用 ASP.NET 设置 URL 路由的 step-by-step 指南非常出色(获得 ASP.NET 路由并运行 - 权威指南)

  • 0

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

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

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

相关问题