【问题标题】:Send parameter to WebApi Action with Postman?使用 Postman 向 WebApi Action 发送参数?
【发布时间】:2017-08-27 18:23:26
【问题描述】:

我有以下操作:

[HttpPost]
public IHttpActionResult GetByUsername(string username)
{
    return Ok(_userBusinessComponent.GetUserByUsername(username));
}

如何通过Postmanusername 发送到此操作@ 我尝试了以下,没有任何反应:

我将动作的签名更改如下:

public IHttpActionResult GetByUsername([FromBody]string username)

这次用户名始终为空!!!

【问题讨论】:

  • 你试过http://localhost:51671/api/User/GetByUsername?username="Mohammad" 吗?
  • @ibubi 有效!谢谢。但是我怎样才能将此参数与 JSON 一起发送到操作。
  • AFAIK 因为你是从 Postman 发送一个对象,你的参数类型应该是一个复杂类型,说它User对象,而不是string username,使用User user

标签: asp.net-web-api postman


【解决方案1】:

正如ibubi 所说,对于以下web api 操作

[HttpPost]
public IHttpActionResult GetByUsername(string username)
{
    return Ok(_userBusinessComponent.GetUserByUsername(username));
}

我必须使用以下代码:

http://localhost:51671/api/User/GetByUsername?username=Moha‌​mmad

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-01
    • 2021-05-09
    • 2014-08-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多