ASP.Net MVC - How to validate the route values? Can I control the routes using the type of the values?

By [)ia6l0 iii

One of the best ways is to use the RouteConstraint to validate the route values and then choose a route to controller. Route values that hold values of integer is the best example. If the route value is passed as string which cannot be converted to an Integer, we suffer with run-time and unhandled exceptions on our code.

The fourth parameter in the MapRoute functions takes Constraints - And here it is
a custom constraint that is applied to the File Id. The regex validates for integer.routes.MapRoute("fileDeleteRoute", "file/delete/{fileId}", new { controller = "file", action = "delete", fileId = 0 },new { fileId = new FileIdConstraint(@"^\d+$")});

The FileIdConstraint class needs to use the IRouteConstraint interface as shown below.

Related FAQs

If you browse your mvc pages, through modern day browsers like Chrome - you will find that there would be additional requests like the ones to the favicon. This is the icon, that appears to the left of the web address in the browser once the page loads.