ASP.NET Web Api HttpResponseException 400 (Bad Request) Hijacked by IIS

前端 未结 6 2099
庸人自扰
庸人自扰 2020-12-15 05:56

I\'m writing a Web API service and trying to return a (400) Bad Request if my ModelState is invalid. I do not want a response body to be attached to this. It appears that II

6条回答
  •  囚心锁ツ
    2020-12-15 06:44

    I had a situation where I was calling a WebApi endpoint from another, and having read that HttpClients should be static I made that change. Worked fine for a few dozen messages but then would get intercepted by the self-host instance which would return a 400 Bad Request. There didn't seem to be anything wrong, especially with the initial success, but my original code would add a default header to the client prior to the call. After I made the client static this would have meant that I was adding the header multiple times to the same client. I changed the code to send create and send an HttpRequestMessage instead of letting the client do it, and added the header to the request. Seems to have solved my problem.

提交回复
热议问题