![](/img/trans.png)
[英]Type or namespace name could not be found (missing using directive or assembly reference?)
[英]The type or namespace name "IRestResponse" could not be found (are you missing a using directive or an assembly reference?)
我在下面IRestResponse
問題:
public async Task<CezanneToken> GetAccessToken()
{
var client = new RestClient(WebConfigurationManager.AppSettings["TokenUrl"]);
var request = new RestRequest();
request.Method = Method.Post;
request.AddHeader("cache-control", "no-cache");
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddParameter("application/x-www-form-urlencoded", "grant_type=client_credentials&client_id=" + WebConfigurationManager.AppSettings["ClientId"] + "&client_secret=" + WebConfigurationManager.AppSettings["ClientSecret"] + "", ParameterType.RequestBody);
IRestResponse response = await client.ExecuteAsync(request);
string serStatus = ((RestResponseBase)response).Content;
CezanneToken details = JsonConvert.DeserializeObject<CezanneToken>(serStatus);
string Token = details.access_token;
return details;
}
IRestResponse
拋出
找不到類型或命名空間名稱“IRestResponse”(是否缺少 using 指令或程序集引用?)我無法使其工作。 IntelliSense 建議使用
RestResponse
> 而不是IRestResponse
。
但是當我使用RestResponse
時,我在響應中收到Bad Request
。
上面的代碼示例是從 Visual Basic“翻譯”而來的,但它在 VB 中工作得很好。 我不知道Bad Request
的問題是否來自使用RestResponse
,但我認為就像在 VB 中一樣需要IRestResponse
。
我也看到有人使用IRestResponse
,但它對我不起作用。 我有using RestSharp;
但我還需要其他東西嗎?
安裝舊版本的 RestSharp NuGet 包 (<=106.15)。
RestSharp 的最新主要版本具有重大更改,這些更改在文檔中進行了描述。
下調到 106.15 對我有用。
根據 RestSharp 文檔https://restsharp.dev/v107/#recommended-usage
IRestResponse 接口已棄用。 作為回報,您將獲得 RestResponse 或 RestResponse 的實例。
在將 nuget 包更新到最新的穩定版本 v.108.03 后,執行請求按預期工作,沒有編譯器錯誤。
RestResponse response = _client.Execute(request);
RestResponse response = await client.ExecuteAsync(request);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.