ASP.NET CORE Web API: Model value is null when doing HTTP Post requests with null Guid

无人久伴 提交于 2019-12-11 16:55:40

问题


I'm facing a problem doing post requests via ASP.NET Core Web API. I'm a bit confused of the articles I read/found while Googling.

So here's my scenario:

I'm trying to do a HTTP Post request to an api endpoint with a complex parameter.

    [HttpPost]
    [Route("api/createstudent")]
    public async Task<ActionResult> CreateStudent([FromBody]Student student)
    {
        // Service logic
    }

Model:

public class Student
{
    public Guid StudentId { get; set; }

    public string StudentName { get; set; }
}

My problem is that every time I make a post request to this endpoint the model value is null if I pass below the object.

{"studentId": null,"studentName": "Sam"}

Whereas If I pass empty Guid as below, I am able to get the model.

{ "studentId": "00000000-0000-0000-0000-000000000000", "studentName": "Sam" }

Is this a bug in ASP.NET Core 2? As I was able to pass null / Empty Guid for StudentId in .NET Framework WEB API.

来源:https://stackoverflow.com/questions/53441075/asp-net-core-web-api-model-value-is-null-when-doing-http-post-requests-with-nul

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!