简体   繁体   English

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

[英]Using of HandleErrorAttribute in ASP.NET MVC application

I have a question about the best way of using HandleErrorAttribute in my MVC 5 application. 我有一个关于在我的MVC 5应用程序中使用HandleErrorAttribute的最佳方法的问题。 As we know, we can add this attribute to global filters like that: 我们知道,我们可以将此属性添加到全局过滤器中:

 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. 但是,如果我在另一个全局授权或动作过滤器中有一些逻辑,这会产生一些异常,那么当第一次抛出异常时,应用程序会尝试重定向到错误视图,同样其他过滤器开始执行并产生相同的异常再次,所以asp.net避免循环终止应用程序。 So what is the best way to use this HandleErrorAttribute to avoid such behavior? 那么使用这个HandleErrorAttribute来避免这种行为的最佳方法是什么? Thanks! 谢谢!

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 fe 编辑:经过一些调试后我发现这不是HandleErrorAttribute的常见行为,所以只有当我使用自定义路由时,才会出现循环

{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. 当过滤器逻辑中出现一些错误时,应用程序尝试重定向到错误视图,但是另一个过滤器逻辑再次开始执行,我甚至在{key}路由参数中看到“错误”值,因此它是不需要的行为。 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. 当我使用默认路由{controller}/{action}这种情况不会发生,而且我没有再次执行任何全局过滤器逻辑而得到错误视图。

You should wrap your action filter logic inside a try catch , then inside the catch block, redirect to the Error view and pass the Exception . 您应该将动作过滤器逻辑包装在try catch ,然后在catch块内,重定向到Error视图并传递Exception

Your only other alternative is to ditch HandleError completely and use the Application_Error event inside Global.asax to manage your error handling. 您唯一的另一种选择是完全抛弃HandleError并使用Global.asax中的Application_Error事件来管理您的错误处理。 That way you can redirect to your Error action inside there regardless of where the error occured. 这样,你可以重定向到你的Error动作里面就有无论出现错误的位置。

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 Matt对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; 然后在每个视图中我添加:Response.StatusCode = 500; or which ever other code I wanted to show back to the client. 或者我希望向客户展示其他代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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