[英]Custom HandleErrorAttribute - How to set result without ASP.NET overriding it?
I'm working on making my ASP.NET MVC apps handle HTTP status codes correctly and when needed display a view for them. 我正在努力使我的ASP.NET MVC应用程序正确处理HTTP状态代码,并在需要时显示它们的视图。 I'm avoiding all of the out of the box error page handling since it isn't semantic (eg the framework always performs a 302 redirect with
customErrors
). 我避免了所有开箱即用的错误页面处理,因为它不是语义上的(例如,框架始终使用
customErrors
执行302重定向)。
I have an AuthorizeAttribute
and ActionFilterAttribute
which are both working correctly and my custom view is displayed. 我有一个
AuthorizeAttribute
和ActionFilterAttribute
都可以正常工作,并且显示了我的自定义视图。 However, my HandleErrorAttribute
isn't working quite right. 但是,我的
HandleErrorAttribute
工作不正常。
public override void OnException(ExceptionContext context) {
HttpStatusCode httpStatusCode = HttpStatusCode.InternalServerError;
var e = context.Exception;
if (e is HttpException) {
var httpException = (HttpException)e;
httpStatusCode = (HttpStatusCode)httpException.GetHttpCode();
}
context.Result = MyResultBuilder.SetViewByHttpCode(context.HttpContext.Request, httpStatusCode, context.Controller.ViewData.ModelState);
}
I am setting the Result
property, but at some point the pipeline is overriding my result as I end up with a Yellow Screen of Death (YSOD) instead of the view result that I defined. 我正在设置
Result
属性,但是在某些时候管道覆盖了我的结果,因为我最终得到的是死亡黄屏(YSOD)而不是我定义的视图结果。
How do I set a custom view result in the HandleErrorAttribute
? 如何在
HandleErrorAttribute
设置自定义视图结果?
尝试将ExceptionHandled属性设置为true;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.