Web API: Content in HttpResponseMessage

前端 未结 1 1444
梦如初夏
梦如初夏 2020-12-05 17:41

In one of my Get request, I want to return an HttpResponseMessage with some content. Currently I have it working as follows:

var header = new MediaTypeHeader         


        
1条回答
  •  北海茫月
    2020-12-05 18:16

    HttpResponseMessage was removed after Beta. Right now, instead of a typed HttpResponseMessage we have a typed ObjectContent

    If you manually create HttpResponseMessage using its default parameterless constructor, there is no request context available to perform content negotiation - that's why you need to specify the formatter, or perform content negotiation by hand.

    I understand you don't want to do that - so use this instead:

    HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, objInstance);
    

    That would create the response message relying on the content negotiation performed against the request.

    Finally, you can read more about content negotiation here On this link

    0 讨论(0)
提交回复
热议问题