ASP.NET MVC2 route restriction - optional number identifier parameter

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

+4
source share
3 answers

I have not tried this, but you can try:

 routes.MapRoute( "DefaultDetails", "{controller}/Details/{id}", new { controller = "Home", action = "Details" }, new { id = @"\d+" } ); routes.MapRoute( "DefaultEdit", "{controller}/Edit/{id}", new { controller = "Home", action = "Edit" }, new { id = @"\d+" } ); routes.MapRoute( "Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); 

I would expect “Customer / Details / 1” to use the first route (which checks the identifier as a number, calling “Customer / Edit / 1” uses the second, which is similar, and calling “Customer / Buy / Orange” uses the third route, which I’m not trying to verify identifiers this way. I understand what you are trying to do correctly? Let me know if this approach works.

+3
source

Add an IgnoreRoute after the one that matches the numeric identifier that will ignore any request for the Details action on the Customer controller:

 routes.MapRoute( "DefaultDigitsId", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index" }, new { id = @"\d+" } // match numeric id ); routes.IgnoreRoute("Customer/Details/{*pathInfo}"); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults ); 
+2
source

Any value that cannot be parsed for a string will make id equal to zero when called, so you can redirect to a page with an error if the identifier is not zero.

Another possibility is to register a global filter that checks if there is a parameter named id and if it is null, then it is redirected to the page with an error

0
source

All Articles