[英]Why does the Web API controller get null data from client through POST request?
I have an AngularJS app sending an object array to a Web API controller method through a POST request. 我有一个AngularJS应用程序,它通过POST请求将对象数组发送到Web API控制器方法。 On the server I have the following method definition:
在服务器上,我具有以下方法定义:
[HttpPost]
[Route("Active")]
public async Task<IHttpActionResult> Post(IList<Media> mediaFiles)
{
try
{
foreach (var file in mediaFiles)
{
await Task.Factory.StartNew(() => PublicData.SetInactiveMedia(file.Id, file.Active));
}
return Ok();
}
catch (ArgumentException e)
{
return BadRequest(e.Message);
}
}
I use the $resource factory in order to interact with the server (but I have to mention that I have also tried with $http and no difference showed up). 我使用$ resource工厂来与服务器进行交互(但我必须指出的是,我也尝试过使用$ http,但未显示出任何区别)。 This is the method:
这是方法:
var activeMedia = $resource('/api/adminhotspotsmedia/active', { save: { method: 'POST' } });
var setActiveMedia = function (mediaFiles) {
activeMedia.save(mediaFiles);
}
The mediaFiles
variable is an array of objects that absolutely match the Media
model on the server. mediaFiles
变量是与服务器上的Media
模型完全匹配的对象数组。
In the Developer's Console I can see this Request Payload associated with the request: 在开发人员控制台中,我可以看到与此请求相关的请求有效负载:
So the array is trying to get to the server, but it cannot. 因此,阵列正在尝试到达服务器,但不能。 The server gets a null value for the list of objects.
服务器获取对象列表的空值。 I will add that I have tried using:
我将补充说我尝试使用:
[FromBody]
tag [FromBody]
标签 dynamic
instead of IList<Media>
dynamic
而不是IList<Media>
IList<object>
instead of IList<Media>
IList<object>
而不是IList<Media>
The problem persists. 问题仍然存在。 What could it be?
会是什么呢?
By default the model binder wont know what to do with the interface unless you have a custom model binder that knows what to expect and what the desired behavior is. 默认情况下,模型绑定器将不知道如何处理接口,除非您有一个自定义模型绑定器,该绑定器知道预期的内容和所需的行为。
Alternatively you can use an actual List<Media>
或者,您可以使用实际的
List<Media>
[HttpPost]
[Route("Active")]
public async Task<IHttpActionResult> Post(List<Media> mediaFiles) { ... }
or array Media[]
或数组
Media[]
[HttpPost]
[Route("Active")]
public async Task<IHttpActionResult> Post(Media[] mediaFiles) { ... }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.