在ASP.NET 5中使用WebClient [英] Using WebClient in ASP.NET 5
问题描述
我正在VS15 beta中工作,并尝试使用WebClient.虽然引用了System.Net,并且智能提示建议WebClient类可用,但是在构建时出现以下错误:
I am working in the VS15 beta and trying to use WebClient. While System.Net is referenced, and the intellisense suggests the WebClient class is available, on build I get the following error:
名称空间"System.Net"中不存在类型或名称空间名称"WebClient"(是否缺少程序集引用?)MyProj.ASP.NET Core 5.0 HomeController.cs
The type or namespace name 'WebClient' does not exist in the namespace 'System.Net' (are you missing an assembly reference?) MyProj.ASP.NET Core 5.0 HomeController.cs
我正在执行以下简单代码:
I am doing the following simplistic code:
var client = new System.Net.
var html = client.DownloadString(url);
当我转到Web Client的定义时,它向我显示了源代码.不太清楚问题出在哪里-WebClient是否移动了?我正在努力寻找解决方案.
When I go to the definition of Web Client, it shows me the source. Not quite sure what the issue is - is WebClient moved? I am struggling to find the resolution.
谢谢!
推荐答案
不确定WebClient
,但是您也可以使用System.Net.Http.HttpClient
发出Web请求.
Not sure about WebClient
, but you can use System.Net.Http.HttpClient
to make web requests as well.
将这些引用添加到project.json
:
"frameworks": {
"aspnet50": {
"frameworkAssemblies": {
"System.Net.Http": "4.0.0.0"
}
},
"aspnetcore50": {
"dependencies": {
"System.Net.Http": "4.0.0-beta-*"
}
}
},
然后是从MVC 6操作方法调用它的方法:
And then here's how to call it from an MVC 6 action method:
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Microsoft.AspNet.Mvc;
namespace WebApplication50.Controllers
{
public class HomeController : Controller
{
public async Task<IActionResult> Index()
{
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("MyClient", "1.0"));
var result = await httpClient.GetStringAsync("http://www.microsoft.com");
...
return View();
}
}
}
这篇关于在ASP.NET 5中使用WebClient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!