I'm trying to validate inputs into the Controller on form variables with the routing rule:
RouteTable.Routes.Add(new Route
{
Url = "Clients/Search/[query]",
Defaults = new { controller = "Clients", action = "Search" },
Validation = new {query=@"\d{2}"},
RouteHandler = typeof(MvcRouteHandler)
});
So the query param can only be a 2 digit number.
When I visit localhost:8080/Clients/Search/QUERY where QUERY is not a 2 digit number, the query parameter is nullified and passed into the controller. If QUERY is a 2 digit number, it's passed into the controller as is.
Seems like this validation should be doing more than just nullifying the input.
I'd prefer it if it actually did something, like pass a validation error into the controllerContext for the action to use. Controller level validation would be good first defense.
I find Regex a really good way of validating url parameters and i really like the fact MVC will validate form variables too. It'd be cool if there was a virtual controller method for receiving validation problems. I can imagine creating Action attributes for specifying general bad input behavior.
Others have implemented validation in model objects ( http://www.dotnetkicks.com/aspnet/How_To_Validation_Using_ASP_NET_MVC) and let the action decide what to do, but I think the Regex's are there for a good reason, and allow for declarable error behavior.