I have a question about best way to 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 involve app to show 'Error' view every time when unhandled exception thrown in any level of app. But, if i have some logic in other global authorize or action filter, that produce some exception, than when exception trhown first time, then app try to redirect to Error View, again other filter begin to executing and produce same exception again, so asp.net to avoid this looping terminate app. So how the best way to use this HandleErrorAttribute to avoid such behavior? Thanks!
这涉及应用程序每次在任何级别的应用程序中抛出未处理的异常时显示“错误”视图。但是,如果我在其他全局授权或动作过滤器中有一些逻辑,那产生一些异常,比第一次出现异常时,那么app尝试重定向到Error View,再次其他过滤器开始执行并再次产生相同的异常,所以asp .net以避免此循环终止应用程序。那么如何使用这个HandleErrorAttribute来避免这种行为的最佳方法呢?谢谢!
Edit: After a some debug i found that this is not usual behavior of HandleErrorAttribute, so this looping happens for me only when i use custom Routes f.e.
编辑:经过一些调试后我发现这不是HandleErrorAttribute的常见行为,所以只有当我使用自定义路由f.e时才会出现这种循环。
{key}/{controller}/{action}
and when some error occurs in filter logic, then app try to redirect to Error View, but again another filter logic begins to exectue and i even see an "Error" value in {key} route parameter, so it is unwanted behavior. When i use default route {controller}/{action}
this not happens and i get exactly to Error View without executing any global filter logic second time.
当过滤器逻辑中出现一些错误时,app尝试重定向到错误视图,但是另一个过滤器逻辑再次开始执行,我甚至在{key}路由参数中看到“错误”值,因此这是不需要的行为。当我使用默认路由{controller} / {action}时,这不会发生,并且我完全得到错误视图而没有第二次执行任何全局过滤器逻辑。
2 个解决方案
#1
9
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. That way you can redirect to your Error
action inside there regardless of where the error occured.
您唯一的另一种选择是完全抛弃HandleError并使用Global.asax中的Application_Error事件来管理您的错误处理。这样,无论发生错误的位置,您都可以重定向到其中的Error操作。
#2
3
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来说Matt是正确的...这是我遵循的例子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;或者我希望向客户展示其他代码。
#1
9
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. That way you can redirect to your Error
action inside there regardless of where the error occured.
您唯一的另一种选择是完全抛弃HandleError并使用Global.asax中的Application_Error事件来管理您的错误处理。这样,无论发生错误的位置,您都可以重定向到其中的Error操作。
#2
3
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来说Matt是正确的...这是我遵循的例子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;或者我希望向客户展示其他代码。