我正在尝试处理以下问题
'A potentially dangerous Request.Form value was detected from the client'
但是,我找不到一个很好的教程来解释如何捕获和处理这个错误。需要说明的是,我想阻止用户在填写我的表单时输入任何类型的标记。大多数处理此问题的文章都建议关闭请求验证。这不是我想做的事。我想捕获错误并将错误回放给用户。任何帮助将不胜感激。
最佳答案
您可以使用异常过滤器捕获错误。像这样的东西:
public class RequestValidationExceptionFilter : IExceptionFilter
{
public void OnException(ExceptionContext filterContext)
{
if(filterContext.Exception is HttpRequestValidationException)
{
filterContext.Result = new RedirectResult("/Error");
filterContext.ExceptionHandled = true;
}
}
}
您可以在异常的Message
中获取更多信息,例如:
A potentially dangerous Request.QueryString value was detected from the client (FilterName="<script>alert("!!")<...").
但这并不是真正要向用户展示的东西。所以最好的办法是重定向到一些通用错误页面。或者您可以将它们发送回当前页面。
关于c# - 处理 'A potentially dangerous Request.Form value was detected from the client' 并通知用户此错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42703724/