HandleErrorAttribute在ASP.NET MVC应用程序中使用 [英] Using of HandleErrorAttribute in ASP.NET MVC application

查看:296
本文介绍了HandleErrorAttribute在ASP.NET MVC应用程序中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题在我的MVC 5应用程序使用HandleErrorAttribute的最佳途径。
我们知道,我们可以增加该属性全局过滤器这样的:

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:

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

这涉及应用显示错误查看每当未处理的异常在应用程序的任何级别抛出的时间。
但是,如果我有在全球其他授权或行动过滤一些逻辑,即产生一些例外,比当例外trhown第一次,然后应用程序尝试重定向到错误查看,再其他过滤器开始执行,并再次产生同样的异常,所以ASP .NET来避免这种循环终止应用程序。
因此,如何最好的方式来使用这个HandleErrorAttribute避免这样的行为?
谢谢!

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!

编辑:
一一些调试后,我发现这不是HandleErrorAttribute的平常行为,所以这种循环发生,我只当我使用自定义的路由f.e。

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.

{key}/{controller}/{action}

和当过滤器逻辑发生了一些错误,那么应用程序尝试重定向到错误查看,但同样另一个过滤逻辑开始exectue,我甚至看到{键}路线参数错误的价值,所以它是不必要的行为。
当我使用默认路由 {控制器} / {行动}
这不正好与我得到确切地查看错误,不执行任何全局过滤逻辑第二次。

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.

推荐答案

您应该包装在尝试 ,那么 Catch中的块,重定向到错误视图并通过例外

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.

您唯一的选择就是抛弃的HandleError 完全并使用的Application_Error 事件中的Global.asax来管理你的错误处理。这样,你可以重定向到您的错误动作里面不管出错的地方。

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.

这篇关于HandleErrorAttribute在ASP.NET MVC应用程序中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆