I’m trying to use route constraints in an Asp.Net MVC Application.
routes.MapRoute( 'theRoute', 'MyAction/{page}', new { controller = 'TheController', action = 'MyAction', page = 1 }, new { page = @'[0-9]' });
When I enter an url like ~/MyAction/aString, an YSOD is shown with an invalid operation exception. What can I do to redirect invalid url to the 404 page?
I know I can solve the issue with a string parameter in the controller action and int.TryParse, but then the route constaint is useless.
How can I choose the exceptiontype that is thrown by the route constraints?
The problem is that you do not have a route that matches the route that ends in a string.
Modify your routes similar to:
and modify your controller
now when you pass a string for the ID, Index2 will be called and you can do whatever you need to do to handle the incorrect parameter.