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
Json
method 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 aJsonResult
and return that:This is all the
Controller.Json
method really does, as can be seen in the source:serializerSettings
can be eitherJsonSerializerOptions
orJsonSerializerSettings
(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
JsonSerializerOptions
without setting any properties, thePropertyNamingPolicy
is 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.