When I put in HTML characters in my form, such as <br />
, ASP.NET throws an internal 500 exception as described here.
A potentially dangerous Request.Form value was detected from the client (Name="<br />").
Ok, so it's protecting me from unencoded characters that could be used for malicious reasons.
The problem is, and this is answered nowhere in my long search, is what to do about it. I.e. my application shouldn't just be throwing a generic internal server error when a user inputs bad characters (what if they're drawing an arrow such as <--).
What would be better is to simply return to the page with a ModelState
error that says "Please don't use HTML characters" or something meaningful.
But how is this to be achieved? The error is thrown way before it gets to my code. Also, I don't want to just turn it off via validateRequest="false"
and then have to validate every single form in my application for HTML characters and return an error.
Is there a way to leave this type of validation enabled but just handle it differently?
Code for clarification:
Model
Public Class SomeModel
Public Property SomeField As String
End Class
Controller
<HttpPost>
Function SomeController(ByVal model As SomeModel)
' model.SomeField contains some HTML characters :O
' but it doesn't matter, since an internal error has occured :(
End Function