.net 如何在BaseAPIController中处理Web API 1.0中的异常

.net 如何在BaseAPIController中处理Web API 1.0中的异常,.net,asp.net-web-api,.net,Asp.net Web Api,我目前正在使用WebAPI 1.0和.NET4.0 我需要一个函数,它可以处理捕获和处理我的基本API中异常的噪音,这样我就不需要在每个RESTful操作中编写它 public int Get(WelcomeTeamNotes note) { try { return _customerService.UpdateNotes(note); } catch (Exception ex)

我目前正在使用WebAPI 1.0和.NET4.0 我需要一个函数,它可以处理捕获和处理我的基本API中异常的噪音,这样我就不需要在每个RESTful操作中编写它

 public int Get(WelcomeTeamNotes note)
    {

        try
        {
            return _customerService.UpdateNotes(note);
        }
        catch (Exception ex)
        {
            log.Error("CustomerController : _customerService.UpdateNotes(note)", ex);
            throw new HttpResponseException(HttpStatusCode.NotFound);
        }
    }

使用Log4net记录错误。我在WebAPI2.0中看到过这种实现,但在1.0实现中没有看到

您可能应该使用异常过滤器。

您可以将此属性放置在某些控制器上,或在全局配置中注册以一次覆盖所有控制器

protected void Application_Start()
{
    GlobalConfiguration.Configuration.Filters.Add(new ExceptionHandlerAttribute());

    ...
}
protected void Application_Start()
{
    GlobalConfiguration.Configuration.Filters.Add(new ExceptionHandlerAttribute());

    ...
}