ASP.NET MVC中的全局错误处理(控制器之外) [英] Global error handling (outside of controller) in ASP.NET MVC

查看:137
本文介绍了ASP.NET MVC中的全局错误处理(控制器之外)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在ASP.NET MVC网站的主页面中放置了以下代码:

  throw new ApplicationException (测试); 

即使在我的控制器上放置了一个[HandleError]属性,这个异常仍然起起伏伏的作用。如何处理这样的错误?我想要能够路由到错误页面,仍然可以记录异常详细信息。



处理这样的事情最好的方法是什么? / p>

编辑:我正在考虑的一个解决方案是添加一个新的控制器:UnhandledErrorController。我可以在Global.asax中放入一个Application_Error方法,然后重定向到这个控制器(它决定如何处理异常)?



注意:defaultRedirect在customErrors web.config元素不会传递异常信息。

解决方案

由于MVC构建在asp.net之上,您应该可以在 web.config ,就像你可以在Web表单中,例如。

 < customErrors mode =OndefaultRedirect =〜/ ErrorHandler/> 


Let's say I put the following code somewhere in a Master page in my ASP.NET MVC site:

throw new ApplicationException("TEST");

Even with a [HandleError] attribute placed on my controller, this exception still bubbles up. How can I deal with errors like this? I would like to be able to route to an Error page and still be able to log the exception details.

What is the best way to deal with something like this?

Edit: One solution I was considering would be to add a a new controller: UnhandledErrorController. Can I put in an Application_Error method in Global.asax and then redirect to this controller (where it decides what to do with the exception)?

Note: the defaultRedirect in the customErrors web.config element does not pass along the exception info.

解决方案

As MVC is built on top of asp.net you should be able to define a global error page in web.config, just like you could in web forms eg.

   <customErrors mode="On" defaultRedirect="~/ErrorHandler" />

这篇关于ASP.NET MVC中的全局错误处理(控制器之外)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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