简体   繁体   English

Method.Put上的RestSharp奇怪的错误

[英]RestSharp weird error on Method.Put

I'm trying to update a resource using RestSharp. 我正在尝试使用RestSharp更新资源。 The API works well because are used in another application so this exclude some routing issues or whatever the problem is on my side not on API one. 该API运作良好,因为是在其他应用程序中使用的,因此可以排除某些路由问题,或者排除我这方面的问题而不是API问题。

Anyways. 无论如何。 My current scenario is that I want to update a specific resource located at host/api/resource/id 我当前的情况是我想更新位于host/api/resource/id的特定资源

This is my current code inside the DataProvider layer 这是我当前在DataProvider层中的代码

public override bool Update(string resource, Dictionary<string, object> properties)
{
    this.request = new RestRequest(resource + "/{id}", Method.PUT);

    for (int i = 0; i < properties.Count; ++i)
    {
        KeyValuePair<string, object> kvp = properties.ElementAt(i);
        if (kvp.Key != "id")
            this.request.AddParameter(kvp.Key, kvp.Value, ParameterType.GetOrPost);
        else
            this.request.AddParameter(kvp.Key, kvp.Value, ParameterType.UrlSegment);
    }

    var response = this.CallApi();
    // ... other stuff
}

This code simply create the request and the correct parameters based on the dictionary that the method received from outside, then it calls the CallApi() method which is this 这段代码只是根据方法从外部接收到的字典创建请求和正确的参数,然后调用CallApi()方法

private IRestResponse CallApi()
{
    var client = new RestClient(BaseUrl);
    var response = client.Execute(this.request);

    if(response.ErrorException != null)
    {
        // Response has some error!
        // ... other stuff
    }

    if(response.StatusCode != System.Net.HttpStatusCode.OK)
    {
        // Response received something different from HTTP status code OK
        // ... other stuff
    }

    return response;
}

CallApi works perfectly for every other call such as GET, POST, DELETE and even PATCH but when I try to use it with Update, and thus using PUT, the response received from client.Execute(this.request) is 405 Method Not Allowed. CallApi非常适合其他所有调用,例如GET,POST,DELETE甚至PATCH,但是当我尝试将其与Update结合使用,从而使用PUT时,从client.Execute(this.request)接收到的响应是405 Method Not Allowed。

After debugging a little bit I figured it out that the respone has a ResponseUri with only the host string instead of host/api/resource/id this seems to be caused by the 经过一点调试后,我发现ResponseUri只有一个ResponseUri字符串,其中包含host字符串而不是host/api/resource/id这似乎是由

this.request = new RestRequest(resource + "/{id}", Method.PUT);

in fact if I remove the /{id} part, the RequestUri has the correct form of host/api/resource , of course without the id, which is wrong anyway because I need the id :-/ 实际上,如果我删除/{id}部分,RequestUri具有正确的host/api/resource ,当然没有id,反正这是错误的,因为我需要id:-/

Does anyone know why this is happening? 有人知道为什么会这样吗?

The problem was at the backslash on new Instance. 问题出在新实例上的反斜杠上。 Just remove the backslash from /{id} and it works 只需从/{id}删除反斜杠就可以了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM