Postman with miltiple params on Web Api C#(在 Web Api C# 上具有多个参数的邮递员)
问题描述
我对这项技术有疑问,
我已经在我的 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:
http://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:
http://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# 上具有多个参数的邮递员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Web Api C# 上具有多个参数的邮递员
基础教程推荐
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- 获取C#保存对话框的文件路径 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- 如果条件可以为空 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- 将数据集转换为列表 2022-01-01
