web-api POST body object always null

前端 未结 26 2907
余生分开走
余生分开走 2020-11-27 15:03

I\'m still learning web API, so pardon me if my question sounds stupid.

I have this in my StudentController:

public HttpResponseMessage          


        
26条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-27 15:19

    Just to add my history to this thread. My model:

    public class UserProfileResource
    {
        public Guid Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Phone { get; set; }
    
        public UserProfileResource()
        {
        }
    }
    

    The above object couldn't be serialized in my API Controller and would always return null. The issue was with Id of type Guid: everytime I passed empty string as an Id (being naive that it will automatically be converted to Guid.Empty) from my frontend I received null object as [FromBody] paramether.

    Solution was either to

    • pass valid Guid value
    • or change Guid to String

提交回复
热议问题