views:

33

answers:

1

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 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!

+1  A: 

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.

er-v