using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Net.Http.Formatting; using System.Net.Http.Headers; using System.Web.Http; namespace WebApi { public class JsonContentNegotiator : IContentNegotiator { private readonly JsonMediaTypeFormatter _jsonFormatter; public JsonContentNegotiator(JsonMediaTypeFormatter formatter) { _jsonFormatter = formatter; } public ContentNegotiationResult Negotiate(Type type, HttpRequestMessage request, IEnumerable formatters) { var result = new ContentNegotiationResult(_jsonFormatter, new MediaTypeHeaderValue("application/json")); return result; } } public static class WebApiConfig { public static void Register(HttpConfiguration config) { // Web API configuration and services //DEBUG config.EnableSystemDiagnosticsTracing(); // Web API routes config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); // Force always json response var jsonFormatter = new JsonMediaTypeFormatter(); //optional: set serializer settings here //config.Services.Replace(typeof(IContentNegotiator), new JsonContentNegotiator(jsonFormatter)); } } }