How to read JSON object to WebAPI

前端 未结 4 1125
走了就别回头了
走了就别回头了 2021-01-14 23:22

I\'ve checked a few similar questions, but none of the answers seem to fit (or dumb it down enough for me). So, I have a really simple WebAPI to check if user with an email

4条回答
  •  一生所求
    2021-01-15 00:02

    Problem: Your current implementation are sending the email as an entity on a GET request. This is a problem because GET requests does not carry an entity HTTP/1.1 Methods Solution: Change the request to a POST

    Now because you are POST'ing the email from your client to your api, you have to change the API implementation to POST:

    public bool Post(UserResponse id)
    

    To make sure your posted entity is bound correctly, you can use [FromBody] like:

    public bool Post([FromBody] UserResponse id)
    

    If you do this (and you have not yet overridden the default model binder), you have to annotate your model like:

    [DataContract]
    public class UserResponse
    {
        [DataMember]
        public string email { get; set; }
    }
    

    I think that is all - hope it works :)

提交回复
热议问题