Reputation: 1969
[HttpPost("method")]
public string Method(int number)
{
return number.ToString();
}
Why number is always 0?
Its possible to use json post with primitive types like this?
My post: { "number" : 3 }
Upvotes: 4
Views: 2598
Reputation: 4482
public class NumberModel
{
public int Number { get; set; }
}
[HttpPost("method")]
public IActionResult Method([FromBody] NumberModel model)
{
return Ok(model.Number);
}
Will auto format it as Json. As Number is an integer, it will return as the string representation of the number. If you just return Ok(model)
it will return an json object like { 'Number' : 3 }
Upvotes: 1
Reputation: 49779
If you post data like { "number" : 3 }
, your action method should be
public class Data
{
public int Number {get; set;}
}
[HttpPost("method")]
public string Method([FromBody] Data data)
{
return data.Number.ToString();
}
Upvotes: 3