title: How do I retrieve the HTTP request method in an ASP.NET WebAPI MediaTypeFormatter?
body: <p>I would like to throw an exception when an ASP.NET WebAPI function returns JSON where the value is an IEnumerable and the HTTP request method is GET - <a href="https://stackoverflow.com/questions/395592/json-security-best-practices">hopefully to stop any JSON being generated where the top level is an array</a>.</p>
  
  <p>I've tried to do this by creating a MediaTypeFormatter. Am I able to do this? Is there another way I can go about doing this? Thanks.</p>
  
  <p>Something like:</p>
  
  <pre><code>public class CustomFormatter : MediaTypeFormatter
  {
      public override Task WriteToStreamAsync(Type type, object value, Stream stream, HttpContentHeaders contentHeaders, TransportContext transportContext)
      {
          // Retrieve value for isGetRequest somehow...
          if (value is IEnumerable &amp;&amp; isGetRequest)
          {
              throw new InvalidOperationException();
          }
          ...
      }
  }
  </code></pre>