While doing some hacking on my own site I encountered (after some googling) a common problem.
"A potentially dangerous Request.Form value was detected from the client may it be a XSS attempt or a malicious character
case 1 : A potentially dangerous Request.Form value was detected from the client (Firstname ="<script> alert("x");...").
case 2 : A potentially dangerous Request.Form value was detected from the client (*).
"
The asp mvc team did a good job catching the error for me but how do I show a nicer error to my users. for example "Something happened, please repeat your steps, if this messegage appears again please contact person x ...".
Solution for asp MVC developers.
<customErrors mode="On" defaultRedirect="~/Error/Index"/>
Optional you might want to put mode="RemoteOnly"
Note for more information on CustomErrors attributes : customErrors Element
It's not MVC-specific. ASP.Net webforms will give you the same error.
I think you should use Custom Error Pages. Custom error pages are defined in Web.config like:
<customErrors mode="RemoteOnly" defaultRedirect="~/Error.aspx"/>
For more information about custom error pages, visit http://aspnetresources.com/articles/CustomErrorPages
Also, you can handle Global.asax Application_Error event to do whatever you want.
Microsoft has a very nice article including source code which does exactly what you want to do (and more). check it out at http://support.microsoft.com/kb/306355
Here is another way that is MVC specific:
This has the advantage that you can show a different error page only for HttpRequestValidationException.
public class HttpRequestValidationExceptionAttribute : FilterAttribute, IExceptionFilter {
public void OnException(ExceptionContext filterContext) {
if (!filterContext.ExceptionHandled && filterContext.Exception is HttpRequestValidationException) {
filterContext.Result = new RedirectResult("~/HttpError/HttpRequestValidationError");
filterContext.ExceptionHandled = true;
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With