I'm trying to force both serialization and deserialization to use SnakeCaseNamingStrategy but no luck.
Here is what I have used in Global.asax.cs:
protected void Application_Start() {
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
HttpConfiguration config = GlobalConfiguration.Configuration;
config.Formatters.JsonFormatter.SerializerSettings = new Newtonsoft.Json.JsonSerializerSettings {
ContractResolver = new DefaultContractResolver {
NamingStrategy = new SnakeCaseNamingStrategy()
}
};
}
I even tried it like the following:
[JsonObject(NamingStrategyType = typeof(Newtonsoft.Json.Serialization.SnakeCaseNamingStrategy))]
public class RegisterBindingModel {
[Required]
[Display(Name = "Email")]
public string Email { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}
Newtonsoft.Json version: 13.0.0.0
Update: the value for "ConfirmPassword" is always null when sending data with the key "confirm_password".
I have to confess that the problem was on my behalf and everything works as expected.
I was sending
text/plainto the API from Postman. And the other mistake was that I was expecting MVC controller to use JSON.NET as deserializer inModelBinderwhich it doesn't use that so obviously it ignores my[JsonProperty()]annotations and even custom configurations inWebApiConfig.csorGlobal.asax.cs.It should be noted that: Only
ApiControlleruses JSON.NET as json serializer/deserializer in its ModelBinder.