在 Web Api C# 上具有多个参数的邮递员

编程入门 行业动态 更新时间:2024-10-23 20:16:50
本文介绍了在 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:

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# 上具有多个参数的邮递员

本文发布于:2023-10-29 08:00:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1539153.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   邮递员   参数   Web   Api

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!