16

I've read ASP.NET Routing… Goodbye URL rewriting? and Using Routing With WebForms which are great articles, but limited to simple, illustrative, "hello world"-complexity examples.

Is anyone out there using ASP.NET routing with web forms in a non-trivial way? Any gotchas to be aware of? Performance issues? Further recommended reading I should look at before ploughing into an implementation of my own?

EDIT Found these additional useful URLs:

4

6 回答 6

7

如何在 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 变成了 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"]);
      }
      
于 2017-11-21T16:22:19.053 回答
4

Not sure if this is your answer but this may get you in the right direction it's Scott Hanselman (MSFT) showing how to get ASP.NET WebForms, ASP.NET MVC and ASP.NET Dynamic Data -- oh and AJAX to work together in harmony.

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

于 2008-10-06T15:51:27.780 回答
3

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

于 2011-03-26T15:40:28.277 回答
1

I saw this podcast linked to from ScottGu's blog the other day which might be useful to you

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

于 2008-10-14T09:40:14.083 回答
0

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

于 2009-01-22T11:02:49.763 回答
0

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

ASP.Net Web 表单中的 URL 路由第 1 部分

ASP.Net Web 表单中的 URL 路由第 2 部分

于 2013-10-07T12:34:43.083 回答