WebApi v2 ExceptionHandler not called

后端 未结 2 1810
情话喂你
情话喂你 2020-12-14 07:27

How comes that a custom ExceptionHandler is never called and instead a standard response (not the one I want) is returned?

Registered like this

2条回答
  •  执念已碎
    2020-12-14 08:11

    The real culprit here is CorsMessageHandler inserted by EnableCors method in message processing pipline. The catch block intercept any exception and convert into a response before it can reach the HTTPServer try-catch block and ExceptionHandler logic can be invoked

    protected override async Task SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        CorsRequestContext corsRequestContext = request.GetCorsRequestContext();
        HttpResponseMessage result;
        if (corsRequestContext != null)
        {
        try
        {
            if (corsRequestContext.IsPreflight)
            {
            result = await this.HandleCorsPreflightRequestAsync(request, corsRequestContext, cancellationToken);
            return result;
            }
            result = await this.HandleCorsRequestAsync(request, corsRequestContext, cancellationToken);
            return result;
        }
        catch (Exception exception)
        {
            result = CorsMessageHandler.HandleException(request, exception);
            return result;
        }
        }
        result = await this.<>n__FabricatedMethod3(request, cancellationToken);
        return result;
    }
    

提交回复
热议问题