Spezifische JSON-Einstellungen pro Controller unter ASP.NET MVC 6
Ich benötige spezifische JSON-Einstellungen pro Controller in meinem ASP.NET MVC 6-WebApi. Ich habe dieses Beispiel gefunden, das für MVC 5 funktioniert (hoffe ich!):Force CamelCase unter ASP.NET WebAPI Pro Controller
using System;
using System.Linq;
using System.Web.Http.Controllers;
using System.Net.Http.Formatting;
using Newtonsoft.Json.Serialization;
public class CamelCaseControllerConfigAttribute : Attribute, IControllerConfiguration
{
public void Initialize(HttpControllerSettings controllerSettings, HttpControllerDescriptor controllerDescriptor)
{
var formatter = controllerSettings.Formatters.OfType<JsonMediaTypeFormatter>().Single();
controllerSettings.Formatters.Remove(formatter);
formatter = new JsonMediaTypeFormatter
{
SerializerSettings = {ContractResolver = new CamelCasePropertyNamesContractResolver()}
};
controllerSettings.Formatters.Add(formatter);
}
}