asp.net-mvc – 自定义异常过滤器在asp.net MVC中没有被击中

我有一个自定义异常过滤器,我用来捕获一个自定义的异常,我写了,但由于某些原因,当我抛出我的异常,它没有得到过滤器.相反,我只是收到一个错误,我的异常未被用户代码处理.任何人都可以提供一些建议/帮助,我应该如何设置?相关代码如下:
// controller    
[CustomExceptionFilter]
    public class SomeController : Controller
    {    
        public SomeController()
        {

        }
        public ActionResult Index()
        {
            SomeClass.someStaticmethod();
            return View();
        }
    }

这是具有customexception属性的控制器

// some class (where exception is being thrown)
public class SomeClass
{
    public static void SomeStaticmethod()
    {
        throw new MyCustomException("Test");
    }
}

那就是抛出异常的类(我的测试)(我也试过直接在控制器上).

// Custom exception filter (want this to catch all unhandled exceptions)
public class CustomExceptionFilter : Filterattribute,IExceptionFilter
{
    public void OnException(ExceptionContext filterContext)
    {
        if (filterContext.Exception.GetType() == typeof(MyCustomException))
        {
            // do stuff
        }
    }
}

这是自定义异常过滤器…它在执行代码并抛出异常时永远不会被访问.相反,我得到上面提到的错误.我读过的一切都表明这是设置这个的正确方法,但是当我在自定义过滤器中设置断点时,它永远不会被击中….

在这里缺少什么?

TIA

解决方法

处理错误后,您需要让过滤器上下文知道已被处理.喜欢这个:
filterContext.ExceptionHandled = true;

这应该在你的“//做东西”部分.

我已经复制了你的代码,并且过滤器被称为罚款.我所做的唯一区别是我添加了exceptionHandled代码,并在该行添加了断点.

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....