本文介绍了ASP.NET MVC Controller.OnException不会被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在那里我重写到Controller.OnException处理方法,以提供一个通用的错误处理某些类型的控制器,能够从这个类继承的基础类(这些API控制器,​​将返回JSON结果)。当异常被控制器提出的onException的方法不会被调用。有谁看到我做错了,或者有更好的方式来处理这种情况?

使用MVC 1.0

的基类:

 公共类APIController:控制器
    {        保护覆盖无效onException的(ExceptionContext filterContext)
        {
           //这是从来没有所谓的
            filterContext.Result =
                新JsonResult();
            base.OnException(filterContext);
        }
    }

继承类:

 公共类myController的:APIController
    {
公共AjaxResult ForcedException()
        {
            抛出新SystemException的();
        }
}


解决方案

如果我正确地理解你的问题 - 你的异常必须标记为你的onException的处理。试试这个:

  filterContext.ExceptionHandled = TRUE;

I have a base controller class where I'm overriding to the Controller.OnException handler method in order to provide a generic error handling for certain types of controllers that will inherit from this class (these are API controllers that will return JSON results). The OnException method never gets called when an exception gets raised by the controller. Does anyone see what I am doing wrong, or is there a better way to handle this situation?

Using MVC 1.0

Base Class:

public class APIController : Controller
    {

        protected override void OnException(ExceptionContext filterContext)
        {
           //This is never called
            filterContext.Result =
                new JsonResult();
            base.OnException(filterContext);
        }


    }

Inheriting Class:

public class MyController : APIController
    {
public AjaxResult ForcedException()
        {
            throw new SystemException();
        }
}
解决方案

If I understand your question correctly - your Exception must be marked as "handled" in your OnException. Try this:

filterContext.ExceptionHandled = true;

这篇关于ASP.NET MVC Controller.OnException不会被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-23 14:57