标签:
from:http://blog.csdn.net/magiccops/article/details/42969363
屏蔽默认返回xml格式:Global文件加:GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
在 WebApiConfig 类中增加方法ConfigureApi,并在 Register 方法最后调用一下 ConfigureApi(config);
增加一个实现IContentNegotiator 接口的类 JsonContentNegotiator
详细如下:
public static void ConfigureApi(HttpConfiguration config)
{}
public class JsonContentNegotiator : IContentNegotiator
{
private readonly JsonMediaTypeFormatter _jsonFormatter;
public JsonContentNegotiator(JsonMediaTypeFormatter formatter)
{
_jsonFormatter = formatter;
}
public ContentNegotiationResult Negotiate(Type type, HttpRequestMessage request, IEnumerable<MediaTypeFormatter> formatters)
{
var result = new ContentNegotiationResult(_jsonFormatter, new MediaTypeHeaderValue("application/json"));
return result;
}
}
标签:
原文地址:http://www.cnblogs.com/94cool/p/4386440.html