C# 在 ASP.NET MVC 应用程序中使用 HandleErrorAttribute

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19025999/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:56:12  来源:igfitidea点击:

Using of HandleErrorAttribute in ASP.NET MVC application

c#asp.net-mvcattributesasp.net-mvc-5

提问by igorGIS

I have a question about the best way of using HandleErrorAttribute in my MVC 5 application. As we know, we can add this attribute to global filters like that:

我有一个关于在我的 MVC 5 应用程序中使用 HandleErrorAttribute 的最佳方式的问题。众所周知,我们可以将此属性添加到全局过滤器中,如下所示:

 filters.Add(new HandleErrorAttribute{View = "Error"});

This involves the app to show the 'Error' view every time when an unhandled exception is thrown in any level of app. But, if I have some logic in another global authorize or action filter, that produces some exception, then when the exception is thrown for first time, the app tries to redirect to the Error View, again other filters begin executing and produce the same exception again, so asp.net to avoid looping terminates the app. So what is the best way to use this HandleErrorAttribute to avoid such behavior? Thanks!

这涉及应用程序每次在任何级别的应用程序中引发未处理的异常时都显示“错误”视图。但是,如果我在另一个全局授权或操作过滤器中有一些逻辑,会产生一些异常,那么当第一次抛出异常时,应用程序尝试重定向到错误视图,其他过滤器再次开始执行并产生相同的异常同样,为了避免循环,asp.net 会终止应用程序。那么使用这个 HandleErrorAttribute 来避免这种行为的最佳方法是什么?谢谢!

Edit:After some debugging I found that this is not the usual behavior of HandleErrorAttribute, so looping happens for me only when I use custom Routes f.e.

编辑:经过一些调试后,我发现这不是 HandleErrorAttribute 的通常行为,因此只有在我使用自定义 Routes fe 时才会发生循环

{key}/{controller}/{action}

and when some error occurs in the filter logic, then the app tries to redirect to the Error View, but again another filter logic begins to exectue and I even see an "Error" value in the {key} route parameter, so it is unwanted behavior. When I use the default route {controller}/{action}this doesn't happen and I get exactly to the Error View without executing any global filter logic a second time.

当过滤器逻辑中出现一些错误时,应用程序尝试重定向到错误视图,但另一个过滤器逻辑再次开始执行,我什至在 {key} 路由参数中看到了“错误”值,因此这是不需要的行为。当我使用默认路由时,{controller}/{action}这不会发生,而且我没有第二次执行任何全局过滤器逻辑就可以准确地进入错误视图。

采纳答案by mattytommo

You should wrap your action filter logic inside a trycatch, then inside the catchblock, redirect to the Errorview and pass the Exception.

您应该将操作过滤器逻辑包装在 a 中trycatch,然后在catch块中,重定向到Error视图并传递Exception.

Your only other alternative is to ditch HandleErrorcompletely and use the Application_Errorevent inside Global.asax to manage your error handling. That way you can redirect to your Erroraction inside there regardless of where the error occured.

您唯一的其他选择是HandleError完全Application_Error放弃并使用Global.asax 中的事件来管理您的错误处理。这样Error,无论错误发生在哪里,您都可以重定向到那里的操作。

回答by KevinB

Matt is right about global.asax... this is the example I followed http://www.digitallycreated.net/Blog/57/getting-the-correct-http-status-codes-out-of-asp.net-custom-error-pages

马特关于 global.asax 是正确的......这是我遵循的例子 http://www.digitallycreated.net/Blog/57/getting-the-correct-http-status-codes-out-of-asp.net-自定义错误页面

Then in each view I added: Response.StatusCode = 500; or which ever other code I wanted to show back to the client.

然后在我添加的每个视图中: Response.StatusCode = 500; 或者我想向客户展示的其他代码。