Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 320 Vote(s) - 3.66 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Force CamelCase on ASP.NET WebAPI Per Controller

#1
In ASP.NET WebAPI, I know you can set the default json formatter to use camel case using [CamelCasePropertyNamesContractResolver()][1] 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?


[1]:

[To see links please register here]

Reply

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


[1]:

[To see links please register here]

Reply

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

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


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#4
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]
Reply

#5
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: .
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through