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:
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);
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.
Thanks!
Not sure about WebClient
, but you can use System.Net.Http.HttpClient
to make web requests as well.
Add these references to the project.json
:
"frameworks": {
"aspnet50": {
"frameworkAssemblies": {
"System.Net.Http": "4.0.0.0"
}
},
"aspnetcore50": {
"dependencies": {
"System.Net.Http": "4.0.0-beta-*"
}
}
},
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();
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With