【问题标题】:Custom errors on MVC 4 Web API before controller action is invoked调用控制器操作之前 MVC 4 Web API 上的自定义错误
【发布时间】:2013-08-01 03:35:21
【问题描述】:

基于this 文章,我实现了自己的ExceptionFilterAttribute 并在GlobalConfiguration 中注册了过滤器,用于在MVC 4 Web API 中进行自定义异常处理。

但是,ExceptionFilterAttribute 仅在控制器操作中引发的异常框架解析请求之后才被调用。

我还想为调用控制器操作之前引发的异常实现自定义错误处理。例如,DelegatingHandler 中引发的异常当前返回以下 HTML:

<!DOCTYPE html>
<html>
    <head>
        <title>Exception of type 'System.Exception' was thrown.</title>
        ...

我需要怎么做才能在这种情况下返回我自己的自定义错误?

【问题讨论】:

    标签: asp.net-mvc-4 asp.net-web-api


    【解决方案1】:

    如果 DelegatingHandler 异常是从您编写的过滤器生成的,那么您可以添加另一个过滤器以将其中一些异常“捕获”为shown in this SO answer

    正如您在 SO 答案中发现并记录的那样,您实际上会从 Web API 管道中“捕获”哪些异常非常有限。我看到尝试执行您所描述的唯一选择是将自定义HttpMessageHandler 编写为demonstrated in this good article

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-01
      • 1970-01-01
      • 2017-10-02
      • 2016-06-23
      • 1970-01-01
      相关资源
      最近更新 更多