Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 4.0 未引发Webapi 2全局异常处理_C# 4.0_Exception Handling_Asp.net Web Api2 - Fatal编程技术网

C# 4.0 未引发Webapi 2全局异常处理

C# 4.0 未引发Webapi 2全局异常处理,c#-4.0,exception-handling,asp.net-web-api2,C# 4.0,Exception Handling,Asp.net Web Api2,我使用WebAPI2为客户端开发服务,管理错误,我们使用ExceptionsFilterAttribute,但正如您所知,在这个级别,并不是所有的异常都被捕获。 在受保护的无效应用程序中出现了一些错误。\u AuthenticateRequestObject sender、EventArgs e和我希望处理这些错误,并向我们的客户端发送自定义消息,向其提供有关错误的更多详细信息,为了解决此问题,我创建了一个GlobalExceptionHandler 在WebApiConfig中,我添加了这一行

我使用WebAPI2为客户端开发服务,管理错误,我们使用ExceptionsFilterAttribute,但正如您所知,在这个级别,并不是所有的异常都被捕获。 在受保护的无效应用程序中出现了一些错误。\u AuthenticateRequestObject sender、EventArgs e和我希望处理这些错误,并向我们的客户端发送自定义消息,向其提供有关错误的更多详细信息,为了解决此问题,我创建了一个GlobalExceptionHandler

在WebApiConfig中,我添加了这一行:

config.Services.Replace(typeof(IExceptionHandler), new GlobalExceptionHandler());
应用程序_AuthenticateRequest引发一些错误,但从未到达GlobalExceptionHandler

你知道我怎样才能解决这个问题吗


提前感谢。

应用程序\u AuthenticateRequest不在Web API管道中。因此,如果在这个方法中抛出异常,那么这些异常可以被Web API异常处理程序捕获,因为异常是在Web API管道启动之前抛出的

有两种方法可以做到这一点:

更改身份验证机制并使用Web API AuthenticationAuthenticationFilter而不是应用程序_AuthenticateRequest

如果这个项目只有与Web API相关的控制器,而不像MVC和all。 或者使用Global.asax.cs文件中的Application_Error捕获Application_AuthenticateRequest中引发的异常

config.Services.Replace(typeof(IExceptionHandler), new GlobalExceptionHandler());