1
我正在使用Postman来测试我编写的PUT Web API方法,但不管我尝试使用强类型化参数,我传递(MyModel)始终为null。Web API Put - 传递的参数为null
控制器动作
[Route("api/myapiname/{id}/{name}")]
[HttpPut]
public HttpResponseMessage Put(int id, string name, MyModel mymodel)
{
*//Do stuff*
}
为MyModel类
public class MyModel
{
public MyModel() { }
private Boolean _b;
public Boolean b { set { _b = value; } get { return _b; } }
private String _comments;
public String comments { set { _comments = value; } get { return _comments; }}
}
URL
http://localhost:37781/api/myapiname/123456/laura
邮差
PUT: URL above
Headers:
Content-Type: application/json
Body:
{
"MyModel":[{"b": true,"comments": "Comments go here"}]
}
我试图改变这一行:
public HttpResponseMessage Put(int id, string name, MyModel mymodel)
到
public HttpResponseMessage Put(int id, string name, [FromBody]MyModel mymodel)
和邮差改变内容类型到的X WWW窗体-urlencoded和我我也尝试更改传递给的内容: = [{“b”:true,“评论”:“评论转到此处”}]
我已经尝试了StackOverflow上提供的各种解决方案,但没有尝试工作,有人可以帮我吗?
感谢您的帮助。 我改变了邮递员请求正文:{“b”:true,“评论”:“评论去这里”}但当我使用VS调试代码对象显示它的属性与默认值/ null,而不是值从邮差传递。 我试着添加[FromBody]属性,这也没有帮助。 – Laura
忽略我最后的评论,我指定了不正确的参数名称。它现在在工作,非常感谢。 – Laura