[SOLVED] ASP.NET API Key's case is messed up

Issue

I use ASP.NET Core 3.1

I want my API to return some data which I format as JSON.

For some reason one sub-object that I want to return is a class instance, with properties formatted as UPPERCASE_SNAKE_CASE.

I expect my response to give me data formatted like this :

{
    "user": {
        "USER_ID": 1,
        "USER_CODE": "xxx",
        "EMAIL": "[email protected]"
    }
}

What I get instead is that some characters before the underscore char are in lowercase :

{
    "user": {
        "useR_ID": 1,
        "useR_CODE": "xxx",
        "email": "[email protected]com"
    }
}

In my Startup.cs file I use the following to format :

public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllers().AddNewtonsoftJson();
        ...
    }

I tried to add this line I read on other posts :

services.AddControllersWithViews().AddJsonOptions(opts => opts.JsonSerializerOptions.PropertyNamingPolicy = null);

Which didnt change anything.

I don’t want all my keys to change into UPPERCASE_SNAKE_CASE, because I am using camelCase, but I want the App to follow how the Key is written in code. When I use a debugger, keys are displayed as I want them to be, it’s only in the client part (Angular or Postman) that the keys are messed up.

Solution

Have you tried adding [JsonProperty("USER_ID")] style attributes to your class properties? –
DavidG
Mar 2 at 11:43

Answered By – Sonny Jayet

Answer Checked By – Mildred Charles (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *