5

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!

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
KerryRitter
  • 51
  • 1
  • 2

2 Answers2

14

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();
        }
    }
}
Eilon
  • 25,582
  • 3
  • 84
  • 102
  • 7
    Might recommend adding `using(...) { ... }` for the HttpClient example, so readers don't forget to dispose properly. – Scott Lin Dec 28 '14 at 06:37
  • 2
    The HttpClient does not need to be disposed of according to it's creators. They do recommend you reuse the HttpClient rather than creating one each time for a request. – Muhammad Rehan Saeed Jun 25 '15 at 14:39
  • 2
    To add references to project.json just use "dnu install System.Net.Http" – Greg Ennis Sep 03 '15 at 02:26
  • @MuhammadRehanSaeed Could you link where is stated that disposal is not needed? It is a disposable class meaning it uses disposable resources (i.e. network connections) therefore has to be disposed. Reuse doesn't mean disposal is not needed, the two are not mutually exclusive. – user3285954 Oct 30 '15 at 14:52
  • See https://stackoverflow.com/questions/15705092/do-httpclient-and-httpclienthandler-have-to-be-disposed – Muhammad Rehan Saeed Oct 30 '15 at 15:47
0

You can still use WebClient if you only target full .NET Framework instead of .NET Core by in your project.json changing:

  "frameworks": {
    "dnx451": { },
    "dnxcore50": { }
  },

to

  "frameworks": {
    "dnx451": { }
  },
markmnl
  • 11,116
  • 8
  • 73
  • 109