Sponsor: Do you build complex software systems? See how NServiceBus makes it easier to design, build, and manage software systems that use message queues to achieve loose coupling. Get started for free.
You may have noticed if you have migrated from ASP.NET Web API to ASP.NET Core, that the default case for serializing output to JSON is now camelCase. If you want to see the history, you can check out this issue on the ASP.NET Core MVC GitHub.Pascal Default
As mentioned, the default is now camelCase. If you need/want all of the JSON output to be in PascalCase, then the solution is pretty simple. All you need to do is specify theDefaultContractResolver
.
Profile
Another option, which I chose to use, is to specify an accept header of application/json but with an profile. Darrel Miller pointed this out to me a couple years ago.Profiles are defined in section 3 of RFC6906:@derek_comartin "profile" is a almost standard way of layering additional semantics on top of an existing media type https://t.co/R90hVZF5s5
— Darrel Miller (Not here, over on the other place) (@darrel_miller) September 1, 2015
The concept of a profile has no strict definition on the Internet or on the web. For the purpose of this specification, a profile can be described as additional semantics that can be used to process a resource representation, such as constraints, conventions, extensions, or any other aspects that do not alter the basic media type semantics.What this could look like for us is defining the accept header of our request as:
Accept: application/json;profile="https://en.wikipedia.org/wiki/PascalCase"
Output Formatter
One of the ways I found to implement this in ASP.NET Core MVC is with an Output Formatter. What we can do is define a new custom output formatter that will be used when the request acceptsapplication/json;profile="https://en.wikipedia.org/wiki/PascalCase"
.
Now to use this new output formatter, we must specify in our ConfigureServices
.