C# 使用 Web 窗体的 ASP.NET 路由

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/174849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 16:41:30  来源:igfitidea点击:

ASP.NET Routing with Web Forms

c#asp.neturlurl-routing

提问by kristian

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

我已经阅读了ASP.NET 路由... 再见 URL 重写?Using Routing With WebForms是很棒的文章,但仅限于简单、说明性的“hello world”复杂示例。

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?

有没有人以一种非平凡的方式使用带有 Web 表单的 ASP.NET 路由?有什么要注意的问题吗?性能问题?在深入研究我自己的实现之前,我应该阅读进一步的推荐阅读吗?

EDITFound these additional useful URLs:

编辑发现这些额外的有用的 URL:

回答by Chad Moran

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.

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

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

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

回答by jammus

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

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

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

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

回答by kristian

Mike Ormond's step-by-step guide to setting up URL routing with ASP.NET is excellent (Getting ASP.NET Routing Up and Running - The Definitive Guide )

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

回答by Roy

Two very useful links for .net 4.0 and ASP.net routing

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

回答by Munjal Pandya

You can find the URL Routing explained in a simple way at the following articles. It provides info like, send request on a Route, retrieve URL parameters on destination page, setting default values for parameters.

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

URL Routing in ASP.Net Web Forms Part - 1

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

URL Routing in ASP.Net Web Forms Part - 2

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

回答by fufuz9000

A simple example of how to use routing in ASP.NET

ASP.NET中如何使用路由的简单例子

  1. Create Empty Web Application
  2. Add first form - Default.aspx
  3. Add second form - Second.aspx
  4. Add third form - Third.aspx
  5. Add to default.aspx 3 buttons -

    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. Read query string on third page

    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write(Request.QueryString["Name"]);
    }
    
  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"]);
    }
    

Now if you run the program, you will be able to navigate to second and third form. This is how it used to be. Let's add routing.

现在,如果您运行该程序,您将能够导航到第二种和第三种形式。这是过去的样子。让我们添加路由。

  1. Add new item - Global.aspx using 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. In 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" }));
    }
    
  3. Modify page load in third.aspx

    protected void Page_Load(object sender, EventArgs e)
    {
        //Response.Write(Request.QueryString["Name"]);
        Response.Write(RouteData.Values["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(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"]);
    }
    

Run the program, Please note that url looks much cleaner - there are not file extensions in it (Second.aspx becomes just Second)

运行程序,请注意 url 看起来更干净 - 里面没有文件扩展名(Second.aspx 变成了 Second)

  1. To pass more then one argument

    • add new button to default.aspx with the following code:

      protected void Button4_Click(object sender, EventArgs e)
      {
          Response.Redirect(GetRouteUrl("FourthRoute", new { Name = "Shoes" , Gender = "Male"}));
      }
      
    • add the following code to global.asax

          routes.MapPageRoute(
            "FourthRoute",
            "Fourth/{Name}-{Gender}",
            "~/Fourth.aspx"
        );
      
    • create Fourth.aspx page with the following page load:

      protected void Page_Load(object sender, EventArgs e)
      {
      Response.Write("Name is: " + RouteData.Values["Name"] + " and Gender is " + RouteData.Values["Gender"]);
      }
      
  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"
        );
      
    • 创建具有以下页面加载的 Four.aspx 页面:

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