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

posted on 2008-03-17 19:35  Q.Lee.lulu  阅读(13458)  评论(10编辑  收藏  举报