I'm posting a very simple JSON object. The WebApi endpoint is successfully hit, but the JSON data I'm passing doesn't seem to get mapped to my Dto object, I'm always getting null. This used to work fine in previous MVC versions. I've tried a couple of things, even adding [FromBody], as suggested in a similar post. Any idea why this is not working? Thanks
Angular Service:
app.service('Issue', [ '$resource',
function($resource) {
return $resource('api/issue', {}, {
save : { method: 'POST', url: 'api/issue/save' }
});
}]);
Angular call:
Issue.save({ id: 1, test: "test_string" });
Issue.save({ Id: 1, Test: "test_string" }); //this call works if I remove the camel case settings from the startup
Dto object:
public class IssueMinDto {
public int Id { get; set; }
public string Test { get; set; }
}
WebAPI method:
[HttpPost("save")]
public void SaveIssue([FromBody]IssueMinDto issue) { //issue is null here
//process data
}
Later Edit: If I remove the camel case settings from the startup (settings below) and I use capital first letters on the call, then it works, but this is not acceptable as all my data across the app was based on that initial setting (lowercase first letter). Is there anything I can improve in my current settings to get this working?
Startup settings - problem should be tackled here:
services.AddMvc().Configure<MvcOptions>(options => {
options.InputFormatters.Clear();
var jsonOutputFormatter = new JsonOutputFormatter();
jsonOutputFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
options.OutputFormatters.Insert(0, jsonOutputFormatter);
});
Later Later Edit
Turns out the following line of code in my startup settings caused this issue:
options.InputFormatters.Clear();
I replaced the settings with the following line and everything works fine now:
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
IdandTestto bind properly? (or alternatively,public int id,public int test)