Tim Liberty
Tim Liberty

Reputation: 2129

Asp.Net core modelbinder null value when POST

I have my action in Controller:

[HttpPost]
public JsonResult SignUp(DTOUser dtoUser)
{
    return Json(new string[] { "value1", "value2" } );          
}

and I have my formatters configured in Startup.cs

 services.AddMvcCore(options => {
                options.InputFormatters.Insert(0, new JilInputFormatter());
                options.OutputFormatters.Insert(0, new JilOutputFormatter());
            });

The problem here is my dtoUser has all variables null until I specify [FromBody]

Why do I have to specify FromBody everytime. This was never required in Asp.Net 4 older version.

How can I get around this issue. It's just an added overhead of adding FromBody to 100s of actions in my project that I will be developing. Any global place where I can add this thing to keep it happy?

Thanks!

Upvotes: 0

Views: 845

Answers (1)

John Bindel
John Bindel

Reputation: 243

It was changed to deal with Cross Site Request Forgery (CSRF) issues. When you don't specify [FromBody] it will not automatically bind to a parameter passed in the body.

Andrew Lock did a great post about this subject: https://andrewlock.net/model-binding-json-posts-in-asp-net-core/

Upvotes: 1

Related Questions