1

I have camelCase by default in my Web API, but I need disable this setting in a specific controller (or in specific http action). This is possible?

Doug
  • 195
  • 1
  • 1
  • 13

1 Answers1

4

I resolved this with this answer: https://stackoverflow.com/a/19959579/3621839

I changed to do the reverse, only:

using System;
using System.Linq;
using System.Web.Http.Controllers;
using System.Net.Http.Formatting;
using Newtonsoft.Json.Serialization;

public class DisableCamelCaseSerialization : 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 DefaultContractResolver() }
        };

        controllerSettings.Formatters.Add(formatter);
    }
}
Community
  • 1
  • 1
Doug
  • 195
  • 1
  • 1
  • 13