开发者

What Controller/Action will this go to?

Assume this is the first route entry:

routes.MapRoute(
  "myRoute",
  "employees/{city}/{pageNumber}",
  new { controller="Employees", action = "List", pageNumber = 1 }
);

If I make the following request

employees/london/2

it gets开发者_JS百科 matched to the following action method:

public ActionResult List(string city) {}

How did that happen? I did not specify "city" in my object defaults:

new { controller="Employees", action = "List", pageNumber = 1 } 

Please explain. Thanks!


The only limitation on RouteData is that it should contains controller and action. Other values can live fine without defaults. For example

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

but if you will use

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)

As you can see in your case routing works just as expected.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜