ASP.NET MVC routes.MapRoute
在一个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 |
routes.MapRoute( "Default", "{controller}/{action}/{id}", new { action = "Index", id = "0" }, new { controller = @"^\w+", action = @"^\w+", id = @"^\d+" }); // Archive/2008-05-07 routes.MapRoute( "BlogArchive", "Archive/{date}", new { controller = "Blog", action = "Archive" }, new { date = @"^\d{4}-\d{2}-\d{2}" }); // Car/bmw.abc routes.MapRoute( "Car", "Car/{make}.{model}", new { controller = "Car", action = "Index" }, new { make = @"(acural|bmw)" }); //必须是提交post url直接回车是get routes.MapRoute( "Book", "Book/Add/{name}", new { controller = "Book", action = "Add" }, new { httpMethod = "POST" }); //后面所有东西都捕获 routes.MapRoute( "CatchIt", "Product/{*values}", new { controller = "Product", action = "Index" });