Post json data in body to web api

后端 未结 2 1379
星月不相逢
星月不相逢 2021-02-14 16:50

I get always null value from body why ? I have no problem with using fiddler but postman is fail.

I have a web api like that:

    [Route(\"api/account/Ge         


        
2条回答
  •  孤独总比滥情好
    2021-02-14 17:23

    You are posting an object and trying to bind it to a string. Instead, create a type to represent that data:

    public class Credentials
    {
        public string Username { get; set; }
        public string Password { get; set; }
    }
    
    [Route("api/account/GetToken/")]
    [System.Web.Http.HttpPost]
    public HttpResponseBody GetToken([FromBody] Credentials value)
    {
        string result = value.Username;
    }
    

提交回复
热议问题