I am working with Umbraco UmbracoApiController and it's really annoying that the data types come back as TitleCase. I would much prefer them to come back as camelCase. Just how I am used to working.
you should add an attribute to the classes/models that you are returning.
e.g
[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
public class MyViewModel {
public string Name { get; set; }
}
this will serialize those classes in camel casing without changing the global default and potentially breaking other code that might rely on it the other way.
public class JsonCamelCaseFormatter : Attribute, IControllerConfiguration
{
public void Initialize(HttpControllerSettings controllerSettings, HttpControllerDescriptor controllerDescriptor)
{
var toRemove = controllerSettings.Formatters.Where(t => (t is JsonMediaTypeFormatter) || (t is XmlMediaTypeFormatter)).ToList();
foreach (var r in toRemove)
{
controllerSettings.Formatters.Remove(r);
}
var jsonFormatter = new JsonMediaTypeFormatter
{
SerializerSettings =
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
}
};
controllerSettings.Formatters.Add(jsonFormatter);
}
}
Then decorate your controller like this.
[JsonCamelCaseFormatter]
public class ResidenceApiController : UmbracoApiController
{
}
Umbraco 8 WebAPI return camelCase
I am working with Umbraco UmbracoApiController and it's really annoying that the data types come back as TitleCase. I would much prefer them to come back as camelCase. Just how I am used to working.
Does anyone know if there is a way to do that?
Thanks
Hi,
you should add an attribute to the classes/models that you are returning.
e.g
this will serialize those classes in camel casing without changing the global default and potentially breaking other code that might rely on it the other way.
Hi Nathan,
This is possible. Here is the code...
Copy this class into your project.
Then decorate your controller like this.
That should be it.
Regards
David
Thats Great,
Thanks Guys.
For anyone looking to do this for umbraco 9, there is a thread here with a solution.
is working on a reply...