I have camel casing configured as the default JSON output from Newtonsoft library. The following line is called during Application_Start:
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
All javascript REST calls to the WebAPIs (using WebAPI 2) work fine and return camel cased JSON strings to the client.
Now, I am using jTable control (http://www.jtable.org) on one of my webpages, and this control requires that JSON payloads be returned in Proper Case. So the question is how can I optionally change the WebAPI to return Proper Case JSON payload even though the default configuration through the Application_Start is set to camel case without changing what the global default setting is? I need Proper Case JSON payload returned just for this one WebAPI call within the application.
I've tried [http://fizzylogic.nl/2014/07/30/changing-the-casing-of-json-properties-in-asp-dot-net-web-api/] this, but I was not able to get the ActionFilterAttribute.OnActionExecuting to fire. So the solution did not work.
I also added the following line of code in the WebAPI method
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new JsonContractResolver(new JsonMediaTypeFormatter());
This works, but Proper Case now becomes the default format for other WebAPI calls.
Here's the WebAPI snippet
public JTableDtoImpl Get(int id, int jtStartIndex = 0, int jtPageSize = 0, string jtSorting = "") {
List<MobileOrderModel> orders = _svc.GetMobileOrders(id);
var dto = new JTableDtoMobileOrdersImpl(orders, jtStartIndex, jtPageSize) {
Message = string.Format("DataSource:{0}", _svc.DataSource)
};
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new JsonContractResolver(new JsonMediaTypeFormatter());
return dto;
}
Problem here is that I now cannot revert back to default camel case formatting since the method has returned by then.
Any other suggestions?