skip to Main Content

I want all JSON responses to use camel case, and tried below code, but it is not working and response is still in Pascal case. I also tried setting [JsonPropertyName("myCamelCaseProperty")] to override, but response still is in Pascal case.

Any suggestions how camel case can be made as default for all responses (all properties)?

builder.Services.Configure<Microsoft.AspNetCore.Http.Json.JsonOptions>(options =>
{
    options.SerializerOptions.PropertyNameCaseInsensitive = false;
    options.SerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
});

builder.Build();

app.UseAuthentication();
app.UseAuthorization();

app.MapControllers();
app.Run();

2

Answers


  1. By Default .Net 6 gives response in Camel Case. Here is the screen shot of my model and response.

    Model

    Response:
    Response

    Login or Signup to reply.
  2. In startup.cs :

    builder.Services.Configure<Microsoft.AspNetCore.Http.Json.JsonOptions>(options =>
    {
        options.SerializerOptions.PropertyNameCaseInsensitive = false;
        options.SerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
    });
    
    builder.Build();
    app.UseAuthentication();
    app.UseAuthorization();
    
    app.MapControllers();
    app.Run();
    

    Then install Microsoft.AspNetCore.Mvc.NewtonsoftJson

    <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="7.0.0" />
    

    In you class reference above using [JsonProperty("customName")]

    public class YourModel
    {
        [JsonProperty("customName")]
        public string CustomName { get; set; }
    
        public string AnotherProperty { get; set; }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search