I have a default route configured in my ASP.NET MVC2 project and would like to add / change it for some of my other controllers. Suppose I have a Customer controller with the expected Details action and id (int) parameter. For instance:
// // GET: /Customer/Details/5 public ActionResult Details(int id) { //... }
How can I add a route that returns 404 if the user enters "not a number"? I tried to add the following default "do" 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 the default route ... All my controllers use Details and Modify, where the id parameter (int) is required. I am wondering if there is a way to do this without specifying multiple routes (that is, something in common) ... And, of course, the goal is that if the user enters something like "/ Customer / Details / apple", he does not throw an error, but leads them to the error page ...
There is also this post that hints at setting a default value, but I'm not sure how to do it ...
source share