gpt4 book ai didi

c# - 从 RestSharp 客户端将 int 列表传递给 Web API

转载 作者:太空狗 更新时间:2023-10-29 22:22:37 26 4
gpt4 key购买 nike

当我使用下面来自 REST Sharp 的代码时,我无法传递 listOfSelectedTicketsIds,它始终为 null。

Rest Sharp .net 客户端请求

 var _stDeveloperApi = new RestClient("http://127.0.0.1/");
var url = string.Format("api/v1/SignalR/MultiClickMode");
var listOfSelectedTicketsIds = new List<int> { 2, 3 };
var request = new RestRequest(url, Method.GET);
request.AddParameter("listOfSelectedTicketsIds", listOfSelectedTicketsIds, ParameterType.GetOrPost);
var response = _stDeveloperApi.Execute(request);

Web API 方法

[HttpGet]
public HttpResponseMessage MultiClickMode(List<int> listOfSelectedTicketsIds)
{
var response = new HttpResponseMessage();

try
{

}
catch (Exception ex)
{
response = Request.CreateResponse(HttpStatusCode.InternalServerError);
}

return response;
}

最佳答案

像这样更改您的客户端代码。

var _stDeveloperApi = new RestClient("http://127.0.0.1/");
var url = string.Format("api/v1/SignalR/MultiClickMode");
var listOfSelectedTicketsIds = new List<int> { 2, 3 };
var request = new RestRequest(url, Method.GET);
listOfSelectedTicketsIds.ForEach(t =>
request.AddParameter(
"listOfSelectedTicketsIds", t, ParameterType.GetOrPost));
var response = _stDeveloperApi.Execute(request);

像这样更改 Web API 操作方法签名。

public HttpResponseMessage MultiClickMode(
[FromUri]List<int> listOfSelectedTicketsIds)

关于c# - 从 RestSharp 客户端将 int 列表传递给 Web API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21271326/

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