gpt4 book ai didi

asp.net-web-api - 从 postman 客户端发送字符串数组

转载 作者:行者123 更新时间:2023-12-01 12:29:14 25 4
gpt4 key购买 nike

我正在使用身份 2.0 的网络 API。我想为一个用户分配多个角色。我正在使用 post man 客户端与 web api 进行通信。在 api 中,我用来获取用户 ID 和角色的方法如下:

public async Task<IHttpActionResult> AddRoleToUser(string userid,[FromUri] string[] selectedRoles)

这里 selectedRoles 是一个数组。从 postman 客户端我传递数组如下: enter image description here

我在 url 中传递用户 ID。我是否从 postman 客户端以正确的格式传递角色数组?调用 api 成功,但 selectedRoles 始终包含空值。我尝试使用下面给出的原始 json,但没有成功

enter image description here

如果我可以将角色作为原始 json 传递,是否可以显示示例

最佳答案

第一个问题:您指定 selectedRoles 数组来自具有此属性的 URI (URL):[FromUri]。您需要删除此属性,因为您是在请求正文中发送它,而不是在 URL 中。

Web API 操作只能从请求正文接收单个参数,从 URL 接收任意数量的参数。因此,您需要将 userid 参数作为查询字符串参数传递,如 ?userid=123123 以及正文中的其他参数。 (您还可以创建一个包含 userid 的路由,或者接收 userid 作为 id 参数并将其作为 URL 段传递,如果你使用的是默认路由)

您还需要在 header 中指定要在正文中发送的信息的格式。因此,您需要包含此 header :Content-Type: application/json,因为您在请求正文中包含了 JSON。

最后,当您从主体发送单个参数时,您可以像这样发送它:

['Admin', 'Employee']

如果你想在你的例子中使用格式,你应该创建一个类在你的操作中用作参数,它看起来像这样:

public class RoleList
{
public string[] selectedRoles { get; set; }
}

你的 Action 应该包括这个作为参数:

public async Task<IHttpActionResult> 
AddRoleToUser(string userid, RoleList roleList)

关于asp.net-web-api - 从 postman 客户端发送字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36006658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com