I have a default route set up in my ASP.NET MVC2 project and would like to add/modify it for some of my other controllers. Lets say I have a Customer controller with Details action that expects and "id" parameter (int). For example:
//
// GET: /Customer/Details/5
public ActionResult Details(int id)
{
//...
}
How can I add a route that will return a 404 if a user enters a "non-number"? I tried adding following "before" default route but it did not work...
routes.MapRoute(
"DefaultDigitsId", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index" },
new { id = @"\d+" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Note, I would like, if possible, to keep default route... All of my controllers use "Details" and "Edit" where "id" (int) parameter is required.. I am wondering if there is a way to accomplish this without having to specify multiple routes (i.e. something generic)...And of course, the goal is that if user enters something like "/Customer/Details/apple" it does not throw an error but takes them to Error page...
There is also this post that hints to setting a default value but I am not sure how to do it...