使用邮递员发送2个或更多生体参数发布请求

时间:2018-05-31 14:56:18

标签: asp.net-web-api asp.net-core postman asp.net-core-2.0 asp.net-core-webapi

我有一个ASP.NET Core 2.0 API方法:

[HttpPost("api/days")]
GetDays([FromBody] DateTime startTime, [FromBody]DateTime endTime)
{

}

我尝试用Postman发送Post请求,但是有一个问题,参数总是有默认值。

这是我的帖子请求如下:

enter image description here

结果:没有工作; API方法中的两个参数都获得默认值。

如果我将API参数更改为:

[HttpPost("api/days")]
GetDays([FromBody] Inputparam param)
{

}

public class Inputparam
{
    public DateTime startTime { get; set; }
    public DateTime endTime { get; set; }
}

这完美无缺!

但我想直接发送参数而不是在包装器对象中。

所以,我回来了第一个API方法,然后我尝试了:

enter image description here

结果:没有工作; API方法中的两个参数都获得默认值。

而这一个:

enter image description here

结果:完美无效;只需要第一个参数(startTime)设置它,第二个参数仍然有默认值。

而这一个:

enter image description here

结果:没有工作; API方法中的两个参数都获得默认值。

我在API中尝试了[FromForm]而不是[FromBody],没有改变。

如果我不在api中使用[FromBody]并通过x-www-form-urlencoded发送完美的请求。

但我需要用JSon发送一个原始的身体。

我怎么能发送2个不同的参数作为原始身体json?

有什么想法吗? 问题在哪里?

2 个答案:

答案 0 :(得分:2)

我发现API中只有一个declare @fireDate date = null -- '01/20/2017' declare @hireDate date = '01/20/2017' declare @deathDate date = '01/20/2017' if (COALESCE(@fireDate, @hireDate, '06/06/2079') = ISNULL(@deathDate, '06/06/2079')) begin select '+' end; if(@fireDate is null and @hireDate is null and @deathDate is null) or (@fireDate = @deathDate) or (@fireDate is null and @hireDate = @deathDate) begin select '+' end; is allowed,这是有道理的。所以答案可能是:没有办法使用[FromBody]属性的2个或更多参数。

答案 1 :(得分:0)

[FromUri]可以完成这项工作。供您参考Microsoft Documentation

public class DateTimes
{
public datetime startdate { get; set; } 
public datetime enddate { get; set; }
}

GetDays([FromBody] DateTimes _date)
{
  //Controller action
}

URI:

api/values/?startdate=2018-06-01&endate=2018-07-01
相关问题