56 lines
1.8 KiB
C#
56 lines
1.8 KiB
C#
using Newtonsoft.Json;
|
|
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<MediaTypeFormatter> 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 }
|
|
);
|
|
|
|
// Dont include properties with null values
|
|
config.Formatters.JsonFormatter.SerializerSettings = new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore };
|
|
|
|
// Force always json response
|
|
//var jsonFormatter = new JsonMediaTypeFormatter();
|
|
//optional: set serializer settings here
|
|
//config.Services.Replace(typeof(IContentNegotiator), new JsonContentNegotiator(jsonFormatter));
|
|
}
|
|
}
|
|
}
|