我有一个这样的方法(这个例子的参数名称故意愚蠢):
[HttpPost]
[Route("rest/myMethod")]
public IHttpActionResult MyMethod(string param1, DateTime param2, DateTime param3, IEnumerable<string> param4, IEnumerable<string> param5 = null)
{
return Ok();
}
但是当我在Swagger中测试时,我得到了这个错误:
{
"message": "An error has occurred.",
"exceptionMessage": "Can't bind multiple parameters ('param4' and 'param5') to the request's content.",
"exceptionType": "System.InvalidOperationException",
"stackTrace": ""
}
一旦我删除了两个集合参数中的一个,这就可以了。
为什么?如何在WebApi中发布多个对象集合?
感谢。
PS:我不知道这是否是一个有用的智能,但是这个WebApi将从AngularJS客户端调用。
答案 0 :(得分:2)
如何在WebApi中发布多个对象集合?
将它们包装在视图模型中:
#!/bin/bash
# run program, invoke in model directory with input files.
# we want to run in the current working directory
#$ -cwd
# we want to run mpi with 4 cores on he same node:
#$ -pe sharedmem 4
# make a generous guess at the time we need
#$ -l h_rt=30:00:00
# force reservation
#$ -R y
# use 4G per process
#$ -l h_vmem=4G
# hold the array
#$ -h
echo I am task $SGE_TASK_ID in $JOB_ID with $SGE_TASK_LAST tasks in total
echo on $HOSTNAME
date
# run our model - set modules, then get the model name
echo "set modules"
. /etc/profile.d/modules.sh
PROGRAMBUILD=/exports/programlocation
. $PROGRAMBUILD/loadModules.sh
modelName=$(basename $PWD)
echo mpirun -np 4 $PROGRAMBUILD/bin/program $modelName
mpirun -np 4 $PROGRAMBUILD/bin/program $modelName
if [ $SGE_TASK_ID == $SGE_TASK_LAST ]
then
echo I am last task
else
# release the next task....
# next task in this array:
next=$((SGE_TASK_ID+1))
echo insert a test that this task in the array job was successful
echo if so, release next task
echo releasing $next
ssh login01.***.uk qrls $JOB_ID.$next
if [[ "$?" -ne 0 ]]; then
echo failed to qrls $pid
fi
fi
你的POST动作将作为参数:
public class MyViewModel
{
public string Param1 { get; set; }
public DateTime Param2 { get; set; }
public DateTime Param3 { get; set; }
public IEnumerable<string> Param4 { get; set; }
public IEnumerable<string> Param5 { get; set; }
}
现在您可以收到以下请求:
[HttpPost]
[Route("rest/myMethod")]
public IHttpActionResult MyMethod(MyViewModel model)
{
return Ok();
}