我想处理HttpRequestValidationExceptions(例如,当将 html 插入表单字段时)并在提交后将用户返回到页面并显示有意义的错误以突出显示问题。
例如,在登录页面上,如果用户在用户名字段中键入了一些无效字符,那么我想捕获HttpRequestValidationException并将用户返回到登录页面,并突出显示用户名字段。
我可以使用OnException方法捕获从HandleErrorAttribute继承的类中的错误。
有没有办法从这里(或任何其他方式)让Controller * ModelState * 添加错误?
注意:我不想关闭验证或重定向到错误页面。
例如:
public override void OnException(ExceptionContext filterContext)
{
if (filterContext.Exception is HttpRequestValidationException)
{
ModelStateDictionary ModelState = <...>
ModelState.AddModelError("Error", "Invalid chars");
filterContext.HttpContext.Response
filterContext.ExceptionHandled = true;
HttpContextBase HttpContext = filterContext.HttpContext;
HttpContext.Response.Redirect(HttpContext.Request.Path);
return;
}
}
提前感谢您能给我的任何帮助!