假设这是第一个路线:
routes.MapRoute(
"myRoute",
"employees/{city}/{pageNumber}",
new { controller="Employees", action = "List", pageNumber = 1 }
);
如果我提出以下请求
employees/london/2
它与以下操作方法匹配:
public ActionResult List(string city) {}
这是怎么发生的?我没有在我的对象默认值中指定“city”:
new { controller="Employees", action = "List", pageNumber = 1 }
请解释一下。谢谢!
答案 0 :(得分:1)
RouteData的唯一限制是它应该包含控制器和动作。没有默认值,其他值可以正常生活。 例如
new { controller="Employees", action = "List", pageNumber = 1 }
employees/london/2 -> Employees.List city=london pageNumber=2
employees/london/ -> Employees.List city=london pageNumber=1 (becouse of defauld)
employees ->this route will not be used, MVC will go find other routs
但如果您将使用
new { controller="Employees", action = "List", city="london" pageNumber = 1 }
employees/london/2 -> same
employees/london/ -> same
employees ->Employees.List city=london(becouse of defauld) pageNumber=1 (becouse of defauld)
正如您在案例中所看到的,路由工作正如预期一样。