参考地址:http://www.cnblogs.com/acles/archive/2013/06/21/3147667.html
1 public static void Register(HttpConfiguration config) 2 { 3 config.Routes.MapHttpRoute( 4 name: "ApiV1", 5 routeTemplate: "datasvc/v1/{action}", 6 defaults: new { controller = "ApiV1" } 7 ); 8 config.EnableSystemDiagnosticsTracing(); 9 10 var jsonFormatter = new JsonMediaTypeFormatter(); 11 config.Services.Replace(typeof(IContentNegotiator), new 12 JsonContentNegotiator(jsonFormatter)); 13 }
----------------------------------------------------------------------------------------
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; } }