CarterCommunity / Carter

Carter is framework that is a thin layer of extension methods and functionality over ASP.NET Core allowing code to be more explicit and most importantly more enjoyable.
MIT License
2.05k stars 172 forks source link

Unable to change the field of the response data body to uppercase #332

Closed Fengtao1314520 closed 1 month ago

Fengtao1314520 commented 6 months ago

when i use our library, i had set

builder.Services.AddCarter(); builder.Services.AddControllers() .AddJsonOptions(options => options.JsonSerializerOptions.PropertyNamingPolicy = null); but output still keep lowcase for first word, like this "userStatus": "active", I need uppercase "UserStatus": "active",

I have to changed respone code like this return Results.Json(result, new JsonSerializerOptions { PropertyNamingPolicy = null });

it can be work, but not i hoped method, i think must have other method can solve it.

jchannon commented 6 months ago

Create your own IResponseNegotiator with the settings you want, Carter will register it in DI automatically

https://github.com/CarterCommunity/Carter/blob/main/src/Carter/Response/DefaultJsonResponseNegotiator.cs

On Fri, 8 Dec 2023 at 09:17, Nate Ford @.***> wrote:

when i use our library, i had set

builder.Services.AddCarter(); builder.Services.AddControllers() .AddJsonOptions(options => options.JsonSerializerOptions.PropertyNamingPolicy = null); but output still keep lowcase for first word, like this "userStatus": "active", I need uppercase "UserStatus": "active",

I have to changed respone code like this return Results.Json(result, new JsonSerializerOptions { PropertyNamingPolicy = null });

it can be work, but not i hoped method, i think must have other method can solve it.

— Reply to this email directly, view it on GitHub https://github.com/CarterCommunity/Carter/issues/332, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAZVJSLXV3PUJJZ2XW3Q23YILLJVAVCNFSM6AAAAABAMLQJAKVHI2DSMVQWIX3LMV43ASLTON2WKOZSGAZTEMRYGE2TQOI . You are receiving this because you are subscribed to this thread.Message ID: @.***>

jchannon commented 1 month ago

You could also define json serializer options using the below, either approaches will work

x.ConfigureHttpJsonOptions(jsonOptions =>
                        {
                            jsonOptions.SerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.KebabCaseUpper;
                        });