If you are using Postman or manually calling your ASP.NET MVC REST api, and getting a null coming through this could be for various reasons:
[Route("Post")]
[HttpPost)
public string Post([FromBody] MyModel model)
{
// do something with model
}
public class MyModel
{
public Guid? Id { get;set; }
}
// Posted JSON:
{
"Id" : ""
}
- [FromBody] is required for HttpPost
- This also enables Swagger (NSwag) calls to be tried out correctly, however it sees everything as a string.
- NSwag needs
[Route]
attributes to recognise your method - NSwag errors can be found out by looking at its JSON file.
- Postman needs
Content-Type: application/json
and a Raw body - Your model properties need to be nullable if they are optional
- Your model needs a parameterless constructor.