[RESOLVED]How can access the “Request” object in MediaTypeFormatter methods?

How can we access HTTPRequestMessage in MediaTypeFormatter Method? I need querystring values in WriteToStreamAsync method. (ex;  /api/MYController.json?select=a,b,c)

public class MyCustomFormatter : MediaTypeFormatter     {

 public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, HttpContent content, TransportContext transportContext)         {

         //Need HttpRequestMessage object to get query string values 

         //HttpContext.Current is always null  and "GetPerRequestFormatterInstance" is calling only once not for each call.

                                                                                                                                                                                                              }                                                                                                                                                                                                                                 
 }

Thanks in advance.

    

Hi,

Maybe we need to use QueryStringMapping extenstion to get querystring.
MediaTypeMappings
 is an interesting concept where a media type formatter can define its preference for a particular media type based on a value in the request (query string, URI fragment, HTTP header).

For more information, please refer to the document:

http://www.strathweb.com/2012/07/everything-you-want-to-know-about-asp-net-web-api-content-negotation/

http://forums.asp.net/t/1776117.aspx?MediaTypeFormatters+based+on+QueryString+or+RouteData+value+instead+of+accept+header

http://byterot.blogspot.com/2012/04/aspnet-web-api-series-part-5.html

Leave a Reply