添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I thought, based on this topic, there is no need to use [FromBody] here, since I want to read the value from the message body, so there is no need to override the default behavior, but, if I don't use [FromBody] , the model that is coming from Angular is null. I'm really confused, why should I use [FromBody] , since I have used the default behavior?

You have to use [FromBody] if you are using HttpPost from your angular side to call the api in .net Core Niladri Jun 9, 2018 at 6:58 Check the link given by stephen below ... it's due to json model binding in .NET Core. For query string you have to use [FromQuery] with httpget. Niladri Jun 9, 2018 at 7:00 In ASP.NET Core 2.1, this attribute may be optional if it's clear the body is assigned to that parameter in a Post. Ray Jun 9, 2018 at 7:02

For anyone seeing this issue .net core 3 - you need to add the [ApiController] to the controller where you extend ControllerBase. The [FromBody] is only needed if you're doing an MVC controller.

This causes the body to get automatically processed in the way you're expecting.

Microsoft documentation for the ApiController attribute

I have seen the same problem in two projects, is there something I missed in the migration documentation from 2.1 -> 2.2 -> 3.0 -> 3.1 Mathias Haugsbø Dec 11, 2019 at 13:55 I was looking all over for this! Thanks. I have a .NET Core Web API 3.1 project and didn't add [ApiController] to my controller. Then I needed [FromBody]. Added [ApiController] now it works without [FromBody]. duyn9uyen Apr 1, 2020 at 21:43 This is not correct. It has to do with which content-type is being used. In order to bind the JSON correctly in ASP.NET Core, you must modify your action to include the attribute [FromBody] on the parameter. This tells the framework to use the content-type header of the request to decide which of the configured IInputFormatters to use for model binding. andrewlock.net/model-binding-json-posts-in-asp-net-core user10285265 Feb 2, 2021 at 18:15

The question you linked to is referring to web-api. You are using core-mvc which has been re-written to merge the pipelines for the previous mvc and web-api versions into one Controller class.

When posting json (as apposed to x-www-form-urlencoded ), the [FromBody] attribute is required to instruct the ModelBinder to use the content-type header to determine the IInputFormatter to use for reading the request.

For a detailed explanation of model binding to json in core-mvc, refer Model binding JSON POSTs in ASP.NET Core .

And here's an alternate approach assuming you need to support both [FromForm] and [FromBody] in your Controller API…

Front-End (Angular Code):

forgotPassword(forgotPassword: ForgotPassword): Observable<number> {
  const params = new URLSearchParams();
  Object.keys(forgotPassword).forEach(key => params.append(key, forgotPassword[key]));
  return this.httpClient.post(`${this.apiAuthUrl}/account/forgotpassword`, params.toString(), { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } });

Back-End (C# Code):

[AllowAnonymous]
[HttpPost("[action]")]
public async Task<IActionResult> ForgotPassword(ForgotPasswordViewModel model) { }

Now your signature can remain the same so it can support both.

And another more permanent approach I thought about while addressing.

https://benfoster.io/blog/aspnet-core-customising-model-binding-conventions.

Hope it helps someone!

See my discussion https://stackoverflow.com/a/75263628/5555938 on [FromBody]. It explains everything in great detail!

But in summary, [FromBody] does NOT accept HTML Form field name-value pairs like [FromForm]. It does NOT accept a traditional HTML form submission! It requires the following:

  • JavaScript POST Request manually sent to the Web API server
  • JavaScript Http Header with JSON mime-type attached
  • JavaScript Http Body with form field extracted data, reformatted and submitted as JSON. Traditional HTML POST name-value pairs will not work!
  • Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.