I have been learning about MVC 3 and I couldn’t figure out one aspect of routing.
If I have a route such as below:
routes.MapRoute("Default", "{controller}/{action}/{ID}");
The route restricts every action to only have one parameter of the name ID!
While there are no restrictions on the name of the controller or actions for this route.
Of course I could define more routes, but there must be a better way to have the route to accept data tokens such as ListID or FieldID.
For example: I have a ListController with two actions:
GetListByID(int listID) and GetFieldByID(int fieldID)
In this case, I have to define two routes because the parameter name of the function is different. Is there a better way to do this? Thanks!
No, your actions can take any amount of parameters, its just that the one parameter in your action named “ID” will contain the value of what is in the url at that position represented by {ID}
If you had this action in the CartController
the url
will call the CartController’s Add method passing in 1234 to the
IDparam, and zero intoquantitybut
will set
quantityas 4You can also have complex arguments
Here’s a good intro
http://weblogs.asp.net/scottgu/archive/2007/12/03/asp-net-mvc-framework-part-2-url-routing.aspx