zoukankan      html  css  js  c++  java
  • 在Asp.net WebForm中实现URL Routing

            在Asp.net 4.0 webform我们也能简单方法实现URL重写。例如,我们原来是这样请求的URL:

    http://localhost:60727/City.aspx?name=ShangHai

          现在变化这样的:

    http://localhost:60727/Cities/ShangHai

          如果您曾接触过ASP.NET MVC,那么你将更加熟悉下面的代码:

    Global.asax:

       1:          void Application_Start(object sender, EventArgs e)
       2:          {
       3:              // Code that runs on application startup
       4:              RegisterRoutes(RouteTable.Routes);
       5:          }
       6:   
       7:          public static void RegisterRoutes(RouteCollection routes)
       8:          {
       9:              routes.MapPageRoute("City", "Cities/{name}", "~/City.aspx");
      10:          }

          同样的,您也可以在HttpModule实现,它是Asp.net核心之一。然后我们来看下City.aspx的codebehind:

       1:      public partial class City : System.Web.UI.Page
       2:      {
       3:          protected void Page_Load(object sender, EventArgs e)
       4:          {
       5:              if (!IsPostBack)
       6:              {
       7:                  object name = String.Empty;
       8:                  // Attempt to get City ID from routing data
       9:                  if (RouteData.Values.TryGetValue("name", out name))
      10:                  {
      11:                      Response.Write(name);
      12:                  }
      13:                
      14:              }
      15:   
      16:          }
      17:      }

        上面我们使用一个属性RouteData,这是.net 4.0 Page类下面的新增加的。 最后的效果将是把参数值ShangHai用Response.Write到页面上。

        希望对您开发有帮助。


    作者:Petter Liu
    出处:http://www.cnblogs.com/wintersun/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
    该文章也同时发布在我的独立博客中-Petter Liu Blog

  • 相关阅读:
    Image Processing and Analysis_8_Edge Detection:Finding Edges and Lines in Images by Canny——1983
    2019年C题 视觉情报信息分析
    Image Processing and Analysis_8_Edge Detection:Theory of Edge Detection ——1980
    Computer Vision_2_Active Shape Models:Active Shape Models-Their Training and Application——1995
    Computer Vision_1_Active Appearance Models:Active Appearance Models——2001
    Computer Vision_1_Active Appearance Models :Active Appearance Models——1998
    这群程序员疯了!他们想成为IT界最会带货的男人
    阿里云视频云正式支持AV1编码格式 为视频编码服务降本提效
    Knative Serverless 之道:如何 0 运维、低成本实现应用托管?
    千呼万唤始出来——DataV私有部署功能
  • 原文地址:https://www.cnblogs.com/wintersun/p/2058010.html
Copyright © 2011-2022 走看看