简体   繁体   English

Custom HandleErrorAttribute-如何在不覆盖ASP.NET的情况下设置结果?

[英]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. 我有一个AuthorizeAttributeActionFilterAttribute都可以正常工作,并且显示了我的自定义视图。 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.

相关问题 在ASP.NET MVC应用程序中使用HandleErrorAttribute - Using of HandleErrorAttribute in ASP.NET MVC application IIS覆盖ASP.NET中的自定义404错误页面 - IIS overriding custom 404 error page in ASP.NET ASP.NET Core 3 - 覆盖自定义的默认 ControllerFactory,在其他情况下如何保存它调用? - ASP.NET Core 3 - Overriding the default ControllerFactory on custom, how save it call in else cases? 如何在ASP.NET中显示自定义404页面而不进行重定向? - How to show a custom 404 page in ASP.NET without redirect? 如何在Asp.Net中将没有Session的Session的值设置为LoginName - How to set value of Session to LoginName without Membership in Asp.Net 在不使用数据集的情况下通过Sql存储过程在ASP.Net MVC模型中保存多个结果集 - Hold Multiple Result Set in ASP.Net MVC Model through Sql Stored Procedure Without Using DataSet 如何在自定义HandleErrorAttribute中获取http状态代码 - How to get the http status code in custom HandleErrorAttribute Asp.Net MVC覆盖类CSS - Asp.Net MVC overriding Class CSS 覆盖ASP.net中的母版页功能 - Overriding a master page function in ASP.net asp.net GridView ImageField设置linq连接的DataImageUrlField结果 - asp.net GridView ImageField set DataImageUrlField result of linq join
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM