[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.



Maybe we need to use QueryStringMapping extenstion to get querystring.
 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:




Leave a Reply