ASP.NET MVC URL Routing 学习
2009-02-11 09:27
435 查看
定义URL Routing
在一个route中,通过在大括号中放一个占位符来定义( { and } )。当解析URL的时候,符号"/"和"."被作为一个定义符来解析,而定义符之间的值则匹配到占位符中。route定义中不在大括号中的信息则作为常量值。
下面是一些示例URL:
通常,我们在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)
public static void RegisterRoutes(RouteCollection routes)
void Application_Start(object sender, EventArgs e)
public static void RegisterRoutes(RouteCollection routes)
public static void RegisterRoutes(RouteCollection routes)
routes.Add(new Route
(
"Category/{action}/{categoryName}"
new CategoryRouteHandler()
)
Defaults = new RouteValueDictionary }
);
}
下面的示例演示了基于上面的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
在一个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中的代码,演示了添加一个包含两个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)
public static void RegisterRoutes(RouteCollection routes)
void Application_Start(object sender, EventArgs e)
public static void RegisterRoutes(RouteCollection routes)
public static void RegisterRoutes(RouteCollection routes)
routes.Add(new Route
(
"Category/{action}/{categoryName}"
new CategoryRouteHandler()
)
Defaults = new RouteValueDictionary }
);
}
下面的示例演示了基于上面的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
相关文章推荐
- ASP.NET MVC 学习心得 (2) - 怎样创建自定义URL Routing
- 3.3 ASP.NET MVC URL Routing 学习
- ASP.NET MVC 学习之路由(URL Routing)
- 【ASP.NET MVC 学习笔记】- 08 URL Routing
- ASP.NET MVC URL Routing 学习
- (ASP.NET MVC学习)url Routing 使用与介绍
- Asp.net MVC 学习心得
- ASP.NET MVC 学习心得 (1) - 怎样创建简单程序
- 从零开始学习 ASP.NET MVC 1.0
- 【转载】ASP.NET MVC Web API 学习笔记---第一个Web API程序
- asp.net mvc 学习
- ASP.NET MVC Framework体验(5):路径选择(URL Routing)(转)
- ASP.NET MVC 学习心得 (4) – 视图控制器
- 蛙蛙推荐:ASP.NET MVC学习笔记
- 《Pro ASP.NET MVC 3 Framework》学习笔记之二十五
- ASP.NET MVC 4学习(一)---准备知识及基础内容
- Asp.net MVC学习思路
- <转>ASP.NET学习笔记之理解MVC底层运行机制
- 学习 ASP.NET mvc 第一天、也可能是最后一天
- ASP.NET MVC 学习笔记(4) 分页处理