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# 上具有多个参数的邮递员
基础教程推荐
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- WPF 模态进度窗口 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
