Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Modelstate error keys to camel case?

How do I set the modelstate keys to camel case in WEB Api .net framework.

I use JsonProperty attribute to set the property names to camel case. Now I want the modelstate to be the same as the json (camel case) how do I achieve that?

like image 972
Arin Avatar asked Apr 19 '17 07:04

Arin


2 Answers

You can do it as follows:

.AddJsonOptions(options =>
{
    options.SerializerSettings.ContractResolver = new DefaultContractResolver
    {
        NamingStrategy = new CamelCaseNamingStrategy
        {
            ProcessDictionaryKeys = true
        }
    };
});
like image 77
Hav Avatar answered Sep 27 '22 18:09

Hav


When configuring MVC in your ConfigureServices(), replace the ContractResolver:

public void ConfigureServices(IServiceCollection services) {
    services.AddMvc()
        .AddJsonOptions(options => options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver());
}
like image 26
Geir Sagberg Avatar answered Sep 27 '22 17:09

Geir Sagberg