Home > database >  Get json data from POST method in asp.net mvc
Get json data from POST method in asp.net mvc

Time:03-11

I got an error while getting json data from POST method, am I doing something wrong

C# Code:

public IActionResult signupapi(UserSignUp user)
    {
        var model = new Models.SignUpModelAPI(HttpContext);
        if (user == null)
        {
            return Content(model.ResponseJsonText(false, string.Format(model.Language("empty"),
                HttpContext.Request.Method, HttpContext.Request.Path.Value), Class.CodeResponse.ERROR), new Microsoft.Net.Http.Headers.MediaTypeHeaderValue("application/json"));
        }
        if (!model.isAllowMethod("POST"))
        {
            return Content(model.ResponseJsonText(false,string.Format(model.Language("notallowmethod"),
                HttpContext.Request.Method,HttpContext.Request.Path.Value),Class.CodeResponse.ERROR),new Microsoft.Net.Http.Headers.MediaTypeHeaderValue("application/json"));
        }
        return Content(JsonConvert.SerializeObject(user));
        }
public class UserSignUp
    {
        public string fullname { get; set; }
        public string username { get; set; }
        public string email { get; set; }
        public string password { get; set; }
    }

And this is the result when i try on reqbin every value i get is null

enter image description here

CodePudding user response:

You need to add FromBody attribute to get your data for the POST operation:

public IActionResult signupapi([FromBody]UserSignUp user)

You can read more on parameter binding on MSDN docs.

  • Related