0

I'm trying to change default json serializer to use Member case instead of camel case in asp.net core 6 minimal API, but the code below does not seem to work

webAppBuilder.Services
    .AddControllers().AddNewtonsoftJson(options =>
    {
        options.UseMemberCasing();
    });

Update I found an easier way to do it than using Newtonsoft. For anyone out there facing a similar issue:

webAppBuilder.Services.Configure<JsonOptions>(opt =>
{
    opt.SerializerOptions.PropertyNamingPolicy = new MemberCasing();
});

public class MemberCasing : JsonNamingPolicy
{
    public override string ConvertName(string name) => name[..1].ToUpper() + name[1..];
}

Any idea on how to achieve member casing json serialization here?

pantonis
  • 5,601
  • 12
  • 58
  • 115
  • 1
    Can you elaborate on how your code "doesn't work"? What were you expecting, and what actually happened? If you got an exception/error, post the line it occurred on and the exception/error details which can be done with a [mre]. Please [edit] your question to add these details into it or we may not be able to help. – gunr2171 Jul 06 '22 at 12:01
  • By saying it does not work I mean camel casing is still being used. I think it is clear.. Camel case is the default one. Minimal reproducing is creating an ASP.NET Core Minimal API from VS2022 templates and add the above code snippet – pantonis Jul 06 '22 at 12:08
  • 2
    Your code is applying Newtonsoft to _Controllers_, not to minimal APIs. – gunr2171 Jul 06 '22 at 12:23
  • Yes and thank you it does but I found an easier way to do it using the System.Text.Json – pantonis Jul 06 '22 at 12:50

0 Answers0