It's probably because ModelState is a dictionary, and the CamelCaseContractResolver only deals with properties of a model.
MVC WebApi CamelCasingPropertyNames Not working on BadRequest
-
19-07-2023 - |
Frage
I have the following in the WebApiConfig.cs file and it works for good request
var jsonFormatter = config.Formatters.JsonFormatter;
jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
The top lines in my controller are
public async Task<IHttpActionResult> ForgotPassword(ForgotPasswordBindingModel model)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
}
The following is my Model being passed into the controller
public class ForgotPasswordBindingModel
{
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[EmailAddress]
[Display(Name = "Email")]
public string Email { get; set; }
}
When my model is serialized back to the client UserName and Email are not camelcased properly
Object {message: "The request is invalid.", modelState: Object}
message: "The request is invalid."
modelState: Object
model.Email: Array[1]
0: "The Email field is not a valid e-mail address."
The object itself ModelState
seems to be camel cased properly and when the request succeeds the resulting object is done properly, however the properties of the modelState for the badrequest do not seem to be camelcased right. modelState.model.email should have to my understanding a lower cased 'E' for email. I thought the Config setting took care of this but it's not. Anyone have any ideas why modelstate is not correctly camelcased when it's serialized back?
Lösung