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

  • 相关阅读:
    解决virtualbox与mac文件拖拽问题
    SNMP收集
    scapy的安装
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    subprocess.call(cmd, shell=True)
    能够把意见说的让人接受是个技能
    (转)Jenkins2.0 Pipeline 插件执行持续集成发布流程
    ansible 与 Jinja2的结合 -- 安装zabbix
    运维自动化平台-背后的设计计划和架构
    命令行获取zabbix最新的issues
  • 原文地址:https://www.cnblogs.com/wintersun/p/2058010.html
Copyright © 2011-2022 走看看