I've used Asp.net MVC for a few years now but this issue I've stumbled upon just a few days ago seems something everydayish and I wonder how come I've never bumped into it. It has to do with Asp.net MVC routing and action method selection. First of all think of default route definition that looks like this:
1: // default application route
2: routes.MapRoute(
3: "Default",
4: "{controller}/{action}/{id}",
5: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
6: );
id
route value is optional? Yes optional. So it should be perfectly feasible to have two action methods: one with the id
parameter and one without it:
public ActionResult Index() { ... }
and
public ActionResult Index(int id) { ... }
But you've probably guessed it? This doesn't work out of the box. You'll get a runtime error stating that there are two matching action methods for the current request. A bit strange? I thought so as well. So let's try and accomplish just that!