我已将我的 Dot Net Core 项目从 2.2 更新到 3.1。这是一个 MVC 项目,我从 MVC 项目中调用了 .net core API,并且所有 post 方法调用都会出错。
这是错误:
'HttpClient' does not contain a definition for 'PostAsJsonAsync'
and no accessible extension method 'PostAsJsonAsync' accepting a first argument of type 'HttpClient'
could be found (are you missing a using directive or an assembly reference?
Run Code Online (Sandbox Code Playgroud)
这是示例代码:
HttpResponseMessage resNews = await client.PostAsJsonAsync("News/GetNews", News);
if (resNews.IsSuccessStatusCode)
{
UiLogger.LogInfo("News api called");
var result = resNews.Content.ReadAsStringAsync().Result;
newsItem = JsonConvert.DeserializeObject<News>(result);
}
Run Code Online (Sandbox Code Playgroud)
有什么解决方案或替代方案吗?谢谢。
这对我有用。
Dot net core 3.x 没有任何 PostAsJsonAsync() 程序集。
并且 Dot Net Core 2. 不受 Microsoft 支持。所以我们不应该使用 3.x 之外的任何东西。不应从 2.x 添加程序集。
可能的替代方案是 PostAsync();
HttpResponseMessage resNews =
await client.PostAsync("News/GetNews", new StringContent(JsonConvert.SerializeObject(Object),
Encoding.UTF8, "application/json"));
If pass only string:
HttpResponseMessage resNews =
await client.PostAsync("News/GetNews", new StringContent("string"));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8158 次 |
| 最近记录: |