snake_case JSON with ASP.NET Core MVC

ASP.NET Core MVC uses camel-cased JSON by default. The property with a name “FirstName” gets serialized into “firstName”. Same, on the way in. There is another style: Snake case. Twitter, for example, uses snake-cased JSON. See this. It is possible to get ASP.NET Core MVC deal with snake-cased JSON. You will just need to change the ConfigureServices method of Startup as follows.

public void ConfigureServices(IServiceCollection services)
    services.AddMvc().AddJsonOptions(jo =>
        jo.SerializerSettings.ContractResolver = new DefaultContractResolver()
            NamingStrategy = new SnakeCaseNamingStrategy()

Now, consider the following controller and DTO classes.

public class ValuesController : ControllerBase
    public IActionResult Get()
        return Ok(new Employee() { FirstName = "Yellow", LastName = "Lemon"});

    public IActionResult Post([FromBody]Employee e)
        return Ok();

public class Employee
    public string FirstName { get; set; }
    public string LastName { get; set; }

HTTP GET on /api/values will produce a JSON like this.


You can also post the same JSON and the parameter in the method Post will be bound correctly to the JSON payload.

POST http://localhost:5000/api/values HTTP/1.1
Host: localhost:5000
Content-Type: application/json
Content-Length: 43


One thought on “snake_case JSON with ASP.NET Core MVC

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.