0

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();
4
  • Can you see the request going to the server? Does it have the correct parameters set? Commented Oct 24, 2015 at 20:07
  • Shouldn't it be Id and Test to bind properly? (or alternatively, public int id, public int test) Commented Oct 24, 2015 at 20:10
  • I've also tried using capital first letters, no luck. Also, this was working fine on previous MVC versions. Commented Oct 24, 2015 at 20:15
  • @lrb: I used fiddler to check that, I can see {"Id":1,"Test":"test_string"} in the request body when dragging the request in the composer Commented Oct 24, 2015 at 20:26

2 Answers 2

1

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();
Sign up to request clarification or add additional context in comments.

Comments

0

Almost, the Json parameters must be equals to the C# parameters

Issue.save({ Id: 1, Test: "test_string"});

Update 1

Try to change $resource with $http

app.service('Issue', ['$http', function ($http) {
    this.save = function (data) {
        return $http.post('api/issue/save', data );
    }
}]);

4 Comments

This doesn't work. I've tried a force refresh (clearing cache). I've updated the topic description with my startup configuration, if it helps.
Thanks for answering. Regarding update 1: I've tried using $http before posting the question here, and still not working. However, I just tried commenting out the camel case setting from the startup, and if I use capitals as you suggested it works (which means my angular is correct) - but this would mean I have to use capital first letter across entire app, which I'm afraid is not an option. I've also tried using lowercase letters on both ends with the settings on. This doesn't work either.
I figured it out, I've replaced my settings with the following line: options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
Interesting I learned something new today for MVC6. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.