与ASP.NET Core中的FromBody混淆 [英] Confused with FromBody in ASP.NET Core

查看:275
本文介绍了与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)

我基于主题,这里不需要使用[FromBody],因为我想从消息正文中读取值,所以不需要覆盖默认行为,但是,如果我不使用[FromBody],则来自Angular的模型为null.我真的很困惑,既然使用了默认行为,为什么还要使用[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核心3的任何人,您都需要将[ApiController]添加到扩展ControllerBase的控制器中. 只有在执行MVC控制器时才需要[FromBody].

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混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆