使用Put和curl调用Dotnet Core Aspnet Rest

时间:2017-02-03 21:44:33

标签: rest curl asp.net-web-api asp.net-core

如何将curl语句写入PUT Dotnet核心aspnet 1.1 rest方法?

我可以让它识别我的整数id而不是我的字符串有效负载。

方法是:

[HttpPut("{id}")]
public IActionResult Put(int id, string name){...

Microsoft docs复制。

当我卷曲时:

curl -H "Content-Type: application/json" \
    -X PUT \
    --data '{"Name":"Tva"}' \
    localhost:5000/api/todo/2

它确实识别URL中的id 2,但不识别Name参数。相反,该方法将其接收为null Web服务器吐出Executing action method WebApplication.Controllers.TodoController.Put (MyWs) with arguments (2, ) - ModelState is Invalid

我还尝试设置[FromBody]代码

[HttpPut("{id}")]
public IActionResult Put(int id, [FromBody] string name){...

并称之为:

curl -H "Content-Type: multipart/form-data;" \
    -X PUT \
    -F "name=Tva" \
    localhost:5000/api/todo/2/

然后我得到了415。

(我确实已经对上面的电话和被叫者进行了排列,但没有找到合适的组合。有些东西丢失了。)

小编辑:
'dotnet new'是updated with RC4也创建了一个web api项目。我还没有调查过我是否按照微软认为的那样写了被调用者 我的SO问题代表但未来可能不那么重要。我会回到这个主题。

1 个答案:

答案 0 :(得分:1)

尝试使用curl:

curl -H "Content-Type: application/json" \
    -X PUT \
    --data '"Tva"' \
    localhost:5000/api/todo/2

或尝试使用包装器模型:

public class MyModel
{
    public string Name { get; set; }
}

[HttpPut("{id}")]
public IActionResult Put(int id, [FromBody] MyModel model){...

并卷曲:

curl -H "Content-Type: application/json" \
    -X PUT \
    --data '{"name": "Tva"}' \
    localhost:5000/api/todo/2