Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Json post int type is always 0

[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 }

like image 733
Felipe Deveza Avatar asked Oct 28 '25 16:10

Felipe Deveza


1 Answers

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();
}
like image 89
Set Avatar answered Oct 30 '25 07:10

Set