【问题标题】:RestSharp RestRequest with Paramaters not working带参数的 RestSharp RestRequest 不起作用
【发布时间】:2016-02-15 09:21:22
【问题描述】:

我正在调用一个 web api。 Exmaple 应该检索名称中带有字母“a”的每个组织。 这是 url,它直接作用于 web api http://localhost/GMSWebServices/api/Organisations/get?name=a

如果我像这样将源代码硬编码到我的调用函数中

        RestRequest request = new RestRequest("Organisations/Get?Name=a");
        // set the response data format
        request.RequestFormat = ReturnFormat;

       var response = _restClient.Execute<List<string>>(request);

这很好用。但是当我使用源是变量并且参数添加不同的格式时 例如

string Source = "Organisations";
RestRequest request = new RestRequest(Source, Method.GET);
        // set the response data format
        request.RequestFormat = ReturnFormat;
        //provide any paramaters
        foreach (RestSharp.Parameter p in WebParamaters)
        {
            request.AddParameter(p);
        }
var response = _restClient.Execute<List<string>>(request);

它不起作用。

我是否以正确的方式使用参数? 我是否需要将“/Get”附加到我的源代码的末尾,我假设 Method.Get 已经处理好了。

我应该如何使用列表中的参数调用源 Get 方法? 我的路由模板应该是什么样子才能使每种方法起作用?

埃里克

【问题讨论】:

  • 那是与Flurl:var response = await url.AppendPathSegment("Organisations/get").SetQueryParam("name", val).GetJsonAsync&lt;List&lt;string&gt;&gt;(); 的单行代码

标签: parameters get restsharp


【解决方案1】:

好的..所以我无法让 AddParameter 工作,但后来我将其更改为 AddQueryParameter 并且它工作了。

我查看了 Restshapr 文档,但我不确定为什么一个有效而另一个无效。

我正在调用 .net web api 服务,但它忽略了 RestSharp 请求对象中的 Parameters 属性。可能web api不支持这种发送参数的方式。

【讨论】:

    【解决方案2】:

    您必须附加“/get”,Method.GET 仅指定请求类型,并且不会向 url 添加任何内容,(因为有时您想要一个 get 方法请求一个没有“get”的 url在里面)。

    参数的用法似乎是对的,所以改成这样:

    string Source = "Organisations/get";
    

    或者如果你在其他地方使用Source而没有get:

    RestRequest request = new RestRequest(Source + "/get", Method.GET);
    

    【讨论】:

    • @user1413844 你的 p 是这样的:p.Name = "name" and p.Value="a"?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-26
    • 1970-01-01
    相关资源
    最近更新 更多