ASP.NET Routing with Web Forms

后端 未结 6 2006
傲寒
傲寒 2020-12-24 03:35

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

相关标签:
6条回答
  • 2020-12-24 04:02

    A simple example of how to use routing in 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"]);
      }
      

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

    Run the program, Please note that url looks much cleaner - there are not file extensions in it (Second.aspx becomes just 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"]);
        }
        
    0 讨论(0)
  • 2020-12-24 04:03

    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 )

    0 讨论(0)
  • 2020-12-24 04:05

    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

    0 讨论(0)
  • 2020-12-24 04:09

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

    • Walkthrough: Using ASP.NET Routing in a Web Forms Application

    • ASP.net Routing

    0 讨论(0)
  • 2020-12-24 04:15

    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 Routing in ASP.Net Web Forms Part - 1

    URL Routing in ASP.Net Web Forms Part - 2

    0 讨论(0)
  • 2020-12-24 04:20

    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

    0 讨论(0)
提交回复
热议问题