Web API Best Approach for returning HttpResponseMessage

后端 未结 3 1143
隐瞒了意图╮
隐瞒了意图╮ 2021-02-03 22:01

I have a Web API project and right my methods always returns HttpResponseMessage.

So, if it works or fails I return:

No errors:

3条回答
  •  借酒劲吻你
    2021-02-03 22:16

    One important note: Don't put content in 204 responses! Not only is it against the HTTP specification, but .NET can actually behave in unexpected manners if you do.

    I mistakenly used return Request.CreateResponse(HttpStatusCode.NoContent, null); and it led to a real headache; future requests from the same session would break due to having a "null" string value prepended to the response. I guess .NET doesn't always do a full clear of the response object for API calls from the same session.

提交回复
热议问题