in asp.net core 3 the returned json will get automatically transformed to camelCase, and I know how to turn this off globally but how do I turn this off for one single action ? I know it was supposed to be something like
return Json(myObj, cfgHere);
but can't find this example anywhere
The
Jsonmethod is part ofController, but isn't part ofControllerBase. If you're usingControllerBase, which is typical for controllers that don't use views, you can new up aJsonResultand return that:This is all the
Controller.Jsonmethod really does, as can be seen in the source:serializerSettingscan be eitherJsonSerializerOptionsorJsonSerializerSettings(if you're using Json.NET). Here's an example that assumes you're using the default,System.Text.Json-based formatters:By creating an instance of
JsonSerializerOptionswithout setting any properties, thePropertyNamingPolicyis left as the default policy, which leaves the property names as-is.If you'd like to use a more declarative approach, which supports content-negotiation, see: Change the JSON serialization settings of a single ASP.NET Core controller.