0

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!

1
  • Can you post your code how you are passing/calling your SignUp() method ? Commented Jul 8, 2017 at 5:24

1 Answer 1

1

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/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.