ASP.NET核心Web API:做与空的Guid的HTTP POST请求时,模型值为null(AS

2019-10-30 12:59发布

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.

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