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对象:

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

publicstaticvoid 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);
}

publicstaticvoid 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);
}

publicstaticvoid 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。仍是看下面的示例:
publicstaticvoid 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
相关文章
相关标签/搜索