Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force CamelCase on ASP.NET WebAPI Per Controller

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?

like image 446
Brad Bamford Avatar asked Nov 13 '13 14:11

Brad Bamford


People also ask

Does the core Web API use camel casing?

If you worked with that example you must have noticed that while serializing data to the client, the ASP.NET Core Web API uses camel casing. That means if your server side C# class is like this :

Is there a camelcasepropertynamescontractresolver for JSON objects?

By default JSON.NET creates JSON objects with the same casing as the corresponding C# objects which is the behavior we are seeing now. However it does ship with a CamelCasePropertyNamesContractResolver which does exactly what we want.

How to stop using camel casing in MVC?

If you run this code with default configuration you will get the following output: As you can see the property names use camel casing. This behavior is similar to the Web API behavior. In order to instruct MVC to stop using camel casing you can write this code in the ConfigureServices ().

Is it possible to achieve CamelCase per controller?

My question is, is it possible do achieve camelcase "Per Controller". 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.


3 Answers

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]
like image 154
Brad Bamford Avatar answered Oct 19 '22 19:10

Brad Bamford


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

like image 10
Kiran Avatar answered Oct 19 '22 19:10

Kiran


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.

like image 4
James Avatar answered Oct 19 '22 18:10

James