35

In ASP.NET WebAPI, I know you can set the default json formatter to use camel case using CamelCasePropertyNamesContractResolver() in the global.aspx which will force ALL json serialization to camel case.

However, I need to be able to set it on a "Per Controller" instance, instead of a Global solution.

Is this possible?

Dariusz Woźniak
  • 9,640
  • 6
  • 60
  • 73
Brad Bamford
  • 3,783
  • 3
  • 22
  • 30

4 Answers4

72

Thanks to @KiranChalla I was able to achieve this easier than I thought.

Here is the pretty simple class I created:

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);

  }
}

Then just add the attribute to any Controller class you want CamelCase.

[CamelCaseControllerConfig]
Brad Bamford
  • 3,783
  • 3
  • 22
  • 30
  • 1
    I believe that this is a better solution that the one pointed to by Kiran. Because this one removes and adds the formatter, it is controller specific. When I used the example pointed to by the marked answer, it changes all my controllers to have the formatters, instead of being controller specific. – Chris Oct 07 '14 at 01:45
  • Thanks, your answer was exactly what I looked. – VikciaR Feb 23 '16 at 12:26
  • Brilliant approach, very useful – GôTô Oct 13 '16 at 16:01
  • 1
    How can we implement this in .net Core? – Prashan Fernando Jan 09 '19 at 09:14
  • @PrashanFernando (or anyone finding this answer) you can find details for .net Core here: https://stackoverflow.com/a/56127866/516138 – Nick Nov 11 '20 at 13:19
  • I've applied the same attribute but now it's applicable for all the controllers even I declared it above only one controller. – zblago Oct 14 '22 at 16:34
11

Yes, it's possible...you can use IControllerConfiguration to define per-controller specific configuration..

This is a sample which describes this scenario. You can quickly take a look at how this interface should be used over here(from the sample).

Kiran
  • 56,921
  • 15
  • 176
  • 161
4

This Stack Overflow answer should be helpful. It shows you how to create an ActionFilter which can be applied to any action where you wish to use CamelCasing.

Community
  • 1
  • 1
James
  • 2,195
  • 1
  • 19
  • 22
  • I looked at before and it is "Per Action". My question is, is it possible do achieve camelcase "Per Controller". – Brad Bamford Nov 13 '13 at 15:24
  • You can use this attribute at the controller and action levels. Although I would recommend adding the line - `_camelCasingFormatter.Indent = true` - to make the output more readable. – James Nov 13 '13 at 15:36
  • This is the best answer, imo – Northstrider Jun 02 '15 at 18:46
0

I know this is pretty old, but I had a problem with the accepted answer because there were other necessary changes to the formatter that were no longer present after removing and re-adding. I did this by just modifying the existing formatter as shown in this Gist: https://gist.github.com/mauricedb/5356933.

xcopy
  • 2,248
  • 18
  • 24