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 - hopefully to stop any JSON being generated where the top level is an array.
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.
Something like:
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 && isGetRequest)
{
throw new InvalidOperationException();
}
...
}
}