How to overcome the error below? I have follow below tutorial but I get the red line that state that does not contain definition of Content
. https://www.youtube.com/watch?v=IVvJX4CoLUY
如何克服下面的错误?我遵循下面的教程,但是我得到了不包含内容定义的红线。https://www.youtube.com/watch?v=IVvJX4CoLUY
private void UploadFile_Cliked(object sender, EventArgs e)
{
var content = new MultipartFormDataContent();
content.Add(new StreamContent(_mediaFile.GetStream()),
"\"file\"",
$"\"{_mediaFile.Path}\"");
var httpClient = new HttpClient();
var uploadServiceBaseAddress = "http://192.168.137.1/pic/";
var httpResponseMessage = httpClient.PostAsync(uploadServiceBaseAddress, content);
RemotePathLabel.Text = httpResponseMessage.Content.ReadAsStringAsync();
}
'Task' does not contain a definition for 'Content' and no extension method 'Content' accepting a first argument of type 'Task' could be found (are you missing a using directive or an assembly reference?)
'Task'不包含'Content'的定义,也没有找到接受'Task'类型的第一个参数的扩展方法'Content'(您是否缺少使用指令或程序集引用?)
This is the error but any idea to solve it?
这是错误,但有办法解决它吗?
I have add this Microsoft.AspNet.WebApi.Client package but still fail
我添加了这个Microsoft.AspNet.WebApi。客户端包仍然失败。
1 个解决方案
#1
2
Make the event handler async and then await functions that return Task derived results as you would have ended up with the same problem when calling ReadAsStringAsync
使事件处理程序异步,然后等待返回任务派生结果的函数,因为在调用ReadAsStringAsync时,您可能会遇到相同的问题
private HttpClient httpClient = new HttpClient();
private async void UploadFile_Cliked(object sender, EventArgs e) {
var content = new MultipartFormDataContent();
content.Add(new StreamContent(_mediaFile.GetStream()),
"\"file\"",
$"\"{_mediaFile.Path}\"");
var uploadServiceBaseAddress = "http://192.168.137.1/pic/";
var httpResponseMessage = await httpClient.PostAsync(uploadServiceBaseAddress, content);
RemotePathLabel.Text = await httpResponseMessage.Content.ReadAsStringAsync();
}
Note that event handlers are an exception to the rule where async void
are allowed
注意,事件处理程序是允许异步void的规则的一个例外。
Reference Async/Await - Best Practices in Asynchronous Programming
参考异步/等待——异步编程中的最佳实践。
#1
2
Make the event handler async and then await functions that return Task derived results as you would have ended up with the same problem when calling ReadAsStringAsync
使事件处理程序异步,然后等待返回任务派生结果的函数,因为在调用ReadAsStringAsync时,您可能会遇到相同的问题
private HttpClient httpClient = new HttpClient();
private async void UploadFile_Cliked(object sender, EventArgs e) {
var content = new MultipartFormDataContent();
content.Add(new StreamContent(_mediaFile.GetStream()),
"\"file\"",
$"\"{_mediaFile.Path}\"");
var uploadServiceBaseAddress = "http://192.168.137.1/pic/";
var httpResponseMessage = await httpClient.PostAsync(uploadServiceBaseAddress, content);
RemotePathLabel.Text = await httpResponseMessage.Content.ReadAsStringAsync();
}
Note that event handlers are an exception to the rule where async void
are allowed
注意,事件处理程序是允许异步void的规则的一个例外。
Reference Async/Await - Best Practices in Asynchronous Programming
参考异步/等待——异步编程中的最佳实践。