MVC controller method not receiving json data from $http post

前端 未结 3 1275
北荒
北荒 2021-01-24 01:17

This might be basic question but I couldn\'t find where the issue is. I am using .Net Core for by back end and angularJS for front end. below is my code.

<
3条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-24 01:42

    You have to use the [FromBody] attribute with a model parameter.

    public class Credentials
    {
        public string Email { get; set; }
        public string Password { get; set; }
    }
    
    public IActionResult Login([FromBody]Credentials creds)
    {
        // do stuff
    }
    

    This is because in ASP.NET Core MVC the default content type is application/x-www-form-urlencoded (for handling HTML form data). The [FromBody] attribute tells the model binder to instead try and create the model from JSON content.

提交回复
热议问题