I have an API method from which I want to return either XML, HTML, plain text, or JSON. The data in question is not an object per-se so I can't just return an object to the framework and have it serialize for me. So I am returning a HttpResponseMessage from
the method with StringContent to specify the actual content to return.
My question is, how can I determine from within the API method which content type the client has requested? I'd like to avoid passing this as a custom parameter to the method because the other methods will use the built-in content type negotiation which
uses the Accept or Content-Type request headers. While I could just access the request headers directly, this seems wrong since content type negotiation can be influenced by other factors as well (such as media type mappings.)
Sorry if the question sounds unclear. I am very new to ASP.NET MVC and the Web API in particular.
josheinstein
0 Points
3 Posts
HttpResponseMessage that plays nice with content negotiation
Mar 28, 2012 06:26 PM|LINK
I have an API method from which I want to return either XML, HTML, plain text, or JSON. The data in question is not an object per-se so I can't just return an object to the framework and have it serialize for me. So I am returning a HttpResponseMessage from the method with StringContent to specify the actual content to return.
My question is, how can I determine from within the API method which content type the client has requested? I'd like to avoid passing this as a custom parameter to the method because the other methods will use the built-in content type negotiation which uses the Accept or Content-Type request headers. While I could just access the request headers directly, this seems wrong since content type negotiation can be influenced by other factors as well (such as media type mappings.)
Sorry if the question sounds unclear. I am very new to ASP.NET MVC and the Web API in particular.