简体   繁体   中英

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. 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 ).

I have an AuthorizeAttribute and ActionFilterAttribute which are both working correctly and my custom view is displayed. However, my HandleErrorAttribute isn't working quite right.

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.

How do I set a custom view result in the HandleErrorAttribute ?

尝试将ExceptionHandled属性设置为true;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM