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

时间:2014-01-21 23:38:36

标签: c# asp.net-web-api restsharp

当我使用REST Sharp中的代码时,我无法传递listOfSelectedTicketsIds它始终为空。

请求Rest .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;
        }

1 个答案:

答案 0 :(得分:10)

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

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)