zoukankan      html  css  js  c++  java
  • ASP.NET MVC URL Routing 学习

    定义URL Routing

    在一个route中,通过在大括号中放一个占位符来定义( { and } )。当解析URL的时候,符号"/""."被作为一个定义符来解析,而定义符之间的值则匹配到占位符中。route定义中不在大括号中的信息则作为常量值。
    下面是一些示例URL

    Valid route definitions

    Examples of matching URL

    {controller}/{action}/{id}

    /Products/show/beverages

    {table}/Details.aspx

    /Products/Details.aspx

    blog/{action}/{entry}

    /blog/show/123

    {reporttype}/{year}/{month}/{day}

    /sales/2008/1/5

     通常,我们在Global.asax文件中的Application_Start事件中添加routes,这确保routes在程序启动的时候就可用,而且也允许在你进行单元测试的时候直接调用该方法。如果你想在单元测试的时候直接调用它,注册该routes的方法必需是静态的同时有一个RouteCollection参数。
    下面的示例是Global.asax中的代码,演示了添加一个包含两个URL参数action categoryName的Route对象:

    protected void Application_Start(object sender, EventArgs e)
    {
        RegisterRoutes(RouteTable.Routes);
    }

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.Add(
    new Route
        (
            
    "Category/{action}/{categoryName}"
             ,
    new CategoryRouteHandler()
        ));
    }


    设置Route参数的默认值
    当你定义个一route的时候,你可以分配一个默认值给route的参数。默认值是当URL中没有包含参数的值的时候使用的。你可以在Route类中通过dictionary来设置Default属性来设置route的默认值:

    void Application_Start(object sender, EventArgs e)
    {
        RegisterRoutes(RouteTable.Routes);
    }
     

    public static void RegisterRoutes(RouteCollection routes)
    {
      routes.Add(
    new Route
      (
        
    "Category/{action}/{categoryName}"
             
    new CategoryRouteHandler()
      )
       
    {
           Defaults
    = new RouteValueDictionary
              
    {{"categoryName", "food"}, {"action", "show"}}
         }

      );
    }
     

     当URL Routing处理URL Request的时候,上面route定义产生的结果如下表所示:  

    URL

    Parameter values

    /Category

    action = "show"
    categoryName = "food"

    /Category/add

    action = "add"
    categoryName = "food"

    /Category/add/beverages

    action = "add"
    categoryName= "beverages"

     
    处理不确定个数的参数
    有时候你需要处理一个URL包含的参数是不确定的URL请求。在你定义route的时候,你可以设置最后一个参数包含一个星号,使最后一个参数匹配URL中剩下的参数。例如:

    query/{queryname}/{*queryvalues}

    当URL Routing处理URL Request的时候,上面route定义产生的结果如下表所示:

    URL

    queryvalues parameter

    /query/select/bikes/onsale

    "bikes/onsale"

    /query/select/bikes

    "bikes"

    /query/select

    Empty string

     
    在匹配URL的时候添加约束
    通过添加约束使URL参数在我们的程序中能更好的工作。废话不多说了,直接看代码:

    void Application_Start(object sender, EventArgs e)
    {
        RegisterRoutes(RouteTable.Routes);
    }
     

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.Add(
    new Route
        (
         
    "{locale}/{year}"
             ,
    new ReportRouteHandler()
        )
          
    {
              Constraints
    = new RouteValueDictionary
             
    {{"locale", "{a-z}{2}-{A-Z}{2}"},{year, @"\d{4}"}}
           }
    );
    }
     

    当URL Routing处理URL Request的时候,上面route定义产生的结果如下表所示:

    URL

    Result

    /en-US

    No match. Both locale and year are required.

    /en-us/2008

    No match. The constraint on locale requires the fourth and fifth characters to be uppercase.

    /en-US/08

    No match. The constraint on year requires 4 digits.

    /en-US/2008

    locale = "en-US"

    year = "2008"


    从Routes中创建URL
    当你想集中控制逻辑来构造URL时,你可以使用routes来产生URLs。通过传递一个dictionary的参数值给RouteCollection对象的GetVirtualPath方法来创建一个URL。GetVirtualPath方法查找RouteCollection对象中第一个route中跟dictionary匹配的参数,匹配的route被用来产生URL。还是看下面的示例:

    public static void RegisterRoutes(RouteCollection routes)
    {
      routes.Add(
    new Route
      (
        
    "Category/{action}/{categoryName}"
             
    new CategoryRouteHandler()
      )
       
    {
           Defaults
    = new RouteValueDictionary {{"categoryName", "food"},
              
    {"action", "show"}}

         }

      );
    }
     

    下面的示例演示了基于上面的route创建的URL

    HyperLink1.NavigateUrl = RouteTable.Routes.GetVirtualPath
      (context,
     
    new RouteValueDictionary {
        {
    "categoryName", "beverages" },
        {
    "action", "summarize" }}
      ).VirtualPath;

    当代码运行的时候,HyperLink1控件将会包含值"Category/summarize/beverages"在NavigateUrl属性中。

    注:以上参考自 http://quickstarts.asp.net/3-5-extensions/mvc/URLRouting.aspx

  • 相关阅读:
    pow()函数结果强制转化为int造成误差的分析
    warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
    博客园鼠标点击特效代码
    codeblocks更改颜色主题
    codeblocks1712设置中文
    SQl
    项目中nodejs包高效升级插件npm-check-updates
    正则表达式的整理(将金钱数变成带有千分位)
    从一个数组中过滤出另外一个数组中相关字段相等的数据
    IONIC3 打包安卓apk详细过程(大量图文)
  • 原文地址:https://www.cnblogs.com/QLeelulu/p/1109893.html
Copyright © 2011-2022 走看看