I have a .net core 2.1 solution comprising a web app, an API and a bunch of libraries.
I am trying to post JSON into a controller in the web project and it is not working - it appears that the properties that I am setting in the JSON are just being set to their default values.
I have tried with and without the [FromBody] attribute and had no luck either way.
This is what I have in the controller
[HttpPost]
public async Task<JsonResult> Search([FromBody] int test)
{
Json(new
{
IThinkYouPassed=test,
});
}
Nothing out of the ordinary there.
I am posting to this using PostMan with the following body :
{
"test":"234"
}
If i put a breakpoint in the action and hit it and I can see that the value of test is 0.
I don't have this issue with the actions in the API project so there must be something missing from the web project - some setup that needs to be done in order for this to work?
I get the same result when using jquery to post the data so Im fairly sure that the issue is with the web app rather than something I am missing in postman.
I thought that maybe the InputFormatter might not be specified but Im told that should happen automatically as part of the UseMVC extension?
Any help with this appreciated.
Your json
is an object that contains the test
field. You should change
public async Task<JsonResult> Search([FromBody] int test)
to
public async Task<JsonResult> Search([FromBody] TestDto testDto)
where TestDto.cs
contains the test field
public class TestDto {
public int Test { get; set; }
}