1
我想拋出一個異常,當一個ASP.NET WebAPI函數返回JSON的值是一個IEnumerable和HTTP請求方法是GET - hopefully to stop any JSON being generated where the top level is an array。如何檢索ASP.NET WebAPI MediaTypeFormatter中的HTTP請求方法?
我試圖通過創建一個MediaTypeFormatter來做到這一點。我能做到嗎?我還有另外一種方法可以做到這一點嗎?謝謝。
喜歡的東西:
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();
}
...
}
}
這是一個很棒的提示 – 2012-08-08 11:29:56