C# WebApi v2 ExceptionHandler不使用HttpPost

C# WebApi v2 ExceptionHandler不使用HttpPost,c#,asp.net-web-api,exceptionhandler,C#,Asp.net Web Api,Exceptionhandler,为什么在调用HTTP方法Post时从不调用自定义ExceptionHandler,而是调用标准响应 这是我的密码 WebApiConfig.cs public static class WebApiConfig { public static void Register(HttpConfiguration config) { // Web API configuration and services //Handler Custom Excepti

为什么在调用HTTP方法Post时从不调用自定义ExceptionHandler,而是调用标准响应

这是我的密码

WebApiConfig.cs

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services
        //Handler Custom Exception
        config.Services.Replace(typeof(IExceptionHandler), new CustomExceptionHandler());

        var cors = new EnableCorsAttribute("*", "*", "*");
        config.EnableCors(cors);

        config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new JsonOutputDateTime());
        // Web API routes
        config.MapHttpAttributeRoutes();

        // Remove the XML formatter (Json Only)
        config.Formatters.Remove(config.Formatters.XmlFormatter);

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/v1/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}
ExceptionHandler.cs

 public class CustomExceptionHandler : System.Web.Http.ExceptionHandling.ExceptionHandler
{
    public override Task HandleAsync(ExceptionHandlerContext context, CancellationToken cancellationToken)
    {
        SystemExceptionReturn returnObj = new SystemExceptionReturn();
        returnObj.responseCode = "xxxx";
        returnObj.responseMessage = "System Error";     
        var response = context.Request.CreateResponse(HttpStatusCode.OK, returnObj);
        context.Result = new ResponseMessageResult(response);

        return base.HandleAsync(context, cancellationToken);
    }

    public virtual bool ShouldHandle(ExceptionHandlerContext context)
    {
        return true;
    }
    private class TextPlainErrorResult : IHttpActionResult
    {
        public HttpRequestMessage Request { get; set; }

        public string Content { get; set; }

        public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
        {
            HttpResponseMessage response =
                             new HttpResponseMessage(HttpStatusCode.InternalServerError);
            response.Content = new StringContent(Content);
            response.RequestMessage = Request;
            return Task.FromResult(response);
        }
    }
}
我的控制器

public class gController : ApiController
{       
    [HttpPost]
    public bool haha(int id)
    {
        bool res = false;
        try
        {
            int ans = id / 0;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        return res;
    }       
}
当我调用localhost:xxxx/api/v1/g/haha时的响应

{
    "Message": "An error has occurred.",
    "ExceptionMessage": "Attempted to divide by zero.",
    "ExceptionType": "System.DivideByZeroException"}
但当我把HttpPost改为HttpGet时,它就为我工作了

请有人帮帮我

对不起,我的英语不好

更新

我发现当localhost中的测试不起作用时,但当部署到IIS时,它起作用
非常感谢您帮助向
本地主机发送POST请求:xxxx/api/v1/g/haha
URL 如果您更改要接收的Id参数[FromBody],它将起作用

        [HttpPost]
        public bool haha([FromBody]int id)
        {
            bool res = false;
            try
            {
                int ans = id / 0;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return res;
        } 

当您将POST更改为get时,您可以共享您得到的响应吗?get响应{“responseCode”:“xxxx”,“responseMessage”:“系统错误”}是否在发送POST请求时在正文中添加id参数?这不是因为
[FromBody]
属性,这更多是已知的CORS问题,虽然我同意OP没有提供带有查询参数的完整链接,因此导致了混乱
        [HttpPost]
        public bool haha([FromBody]int id)
        {
            bool res = false;
            try
            {
                int ans = id / 0;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return res;
        }