How to add request validation errors to ModelStateDictionary in ASP.NET MVC?

Posted by Morten Christiansen on Stack Overflow See other posts from Stack Overflow or by Morten Christiansen
Published on 2010-05-19T11:13:08Z Indexed on 2010/06/02 7:03 UTC
Read the original article Hit count: 394

Investigating the security of a system I'm building with ASP.NET MVC 2 led me to discover the request validation feature of ASP.NET - a very neat feature, indeed. But obviously I don't just want to present the users with the Yellow Screen of Death when they enter data with HTML in, so I'm out to find a better solution.

My idea is to find all the fields that have invalid data and add them to the ModelStateDictionary before invoking the action such that they automatically appear in the UI as error messages. After googling this a bit it appears that no one have implemented this before which I find puzzling since it seems so obvious. Does anyone here have a suggestion on how to do this? My own idea is to supply a custom ControllerActionInvoker to the controller, as described here, that somehow checks for this and modifies the ModelStateDictionary but I'm stuck on how to do this last bit.

Just catching HttpRequestValidationException exceptions does not seem a useful approach since it does not actually contain all the information I need.

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about request-validation