我对这项技术有疑问,
我已经在我的 web api 控制器中创建了一个允许我创建用户的条目:
I already have created a entry in my web api controller that allows me to create users:
public IHttpActionResult PostUser(User user)我可以像这样使用邮递员使用这个休息服务:
and I can consume this rest service with postman like this:
现在我想创建一个类似的条目,但这次使用 2 个参数,如下所示:
Now I want to create a similar entry but this time with 2 parameters, like this:
public IHttpActionResult PutUser(int id, User user)我的问题是我无法通过 POSTMAN 实现此方法
我已经试过了:
- 在邮递员石南花上添加我的id"参数
- 在 postman body-form-data 上添加我的id"参数
- 在 postman body-x-www-form-urlencoded 上添加我的id"参数
- 在 json 代码前后添加我的id"参数作为分隔符 ['&', ','] 和分配字符 [':', '='].
这些都不起作用,我的想法已经用完了.
none of these worked and I ran out of ideas.
有人知道如何正确调用此服务吗?
does anyone knows how to invoke this service properly ?
最好的问候!
推荐答案你应该给你的方法添加属性,像这样:
You should add attributes to your method, as such:
public IHttpActionResult PutUser(int id, [FromBody] User user)这表示 user 参数是从请求正文中接收的.接下来,您可以使用以下网址:
This indicates the user parameter is received from the request body. Next, you can use the following URL:
localhost:15423/api/users?id=<your_id>如果您将 [FromUri] 添加到 id 参数和 [Route("{id}")] 的方法,你可以使用:
And in favor of full on REST use, if you add [FromUri] to the id parameter, and a [Route("{id}")] to the method, you could use:
localhost:15423/api/users/5其中 5 可以替换为您的 id.
Where 5 can be replaced by your id.
(两个请求都应在正文中包含 User 对象)
(Both requests should include the User object in the body)
更多推荐
在 Web Api C# 上具有多个参数的邮递员
发布评论