Confused with FromBody in ASP.NET Core(与 ASP.NET Core 中的 FromBody 混淆)
问题描述
我有以下 WEB API 方法,并且有一个带有 Angular 的 SPA 模板:
I have the following WEB API method, and have a SPA template with Angular:
[HttpPost]
public IActionResult Post([FromBody]MyViewModel model)
我认为,基于 this 主题,这里不需要使用 [FromBody] ,因为我想从消息体中读取值,所以不需要覆盖默认行为,但是,如果我不这样做't 使用 [FromBody],来自 Angular 的模型为空.我真的很困惑,既然我使用了默认行为,为什么还要使用 [FromBody]?
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?
推荐答案
对于任何看到此问题的 .net core 3 - 您需要将 [ApiController] 添加到扩展 ControllerBase 的控制器中.[FromBody] 仅在您使用 MVC 控制器时才需要.
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 文档ApiController 属性
这篇关于与 ASP.NET Core 中的 FromBody 混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:与 ASP.NET Core 中的 FromBody 混淆
基础教程推荐
- 全局 ASAX - 获取服务器名称 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
