I have just spent way longer than I should have done trying to perform a simple GET request to a web api in ASP.net 5.0 via HttpClient.
There is a lot of change in ASP.net 5.0 in terms of the way packages and dependencies are managed, also there are a lot of moving parts right now since the platform is still work in progress which has made this seemingly simple task, very difficult to achieve.
I will not go into the detail of these changes here, if you want an update on the changes in vNext, check out http://www.asp.net/vnext. Instead, I will aim to answer the simple question of “How do i use HttpClient in ASP.net 5?”.
I’ll assume that you already have Visual Studio 2015 and some awareness of what is new in ASP.net 5.0. I’ll also assume you have created a new ‘ASP.net 5 Preview Templates > Web Site’ project.
Step 1 – Enable the ASP.net nightly builds Nuget repository
One of the big changes in ASP.net 5.0 is that many of the features we used to rely on in ASP.net 4.5 have now been separated out into NuGet packages which need to be separately installed.
At this time (June 2015), many of these packages are still in beta and not available via the main NuGet sources. HttpClient is part of the ‘Microsoft.Net.Http.Client’ package, which right now is only available via the ASP.Net team’s nightly build repository (I’m quite certain this will change by the time ASP.net 5.0 ships).
To add ‘Microsoft.Net.Http.Client’, you need to add a new NuGet source in Visual Studio to use the nightly build repository.
1) Add a new NuGet source in Visual Studio to point to https://www.myget.org/F/aspnetvnext/ (you can see the site associated with this feed at https://www.myget.org/gallery/aspnetvnext). You can do this via Tools > Options > Nuget Package Manager > Package Sources
2) Make sure this is the top of the list too otherwise it will not work
Step 2 – Add the ‘Microsoft.Net.Http.Client’ package
There are a few ways to add packages in Visual Studio 2015, but this is a great chance to look at the newest way (and probably the cleanest I think), this is via a new file called Project.json.
Project.json is a new file that is included with all ASP.net projects to manage the dependencies, framework versions etc. Read more about this here: http://docs.asp.net/en/latest/dnx/overview.html. You’ll find this in the root of your project.
If you add or remove things to this file, when you save, Visual Studio will automatically download or remove the packages that are referenced #VeryCool.
1) In the ‘Dependencies’ section, add “Microsoft.Net.Http.Client”: “1.0.0-beta3-10053” (this version may well have increased by the time you read this, if so, just choose the latest version. When you type :, it will give you a drop down of available versions. If you don’t see this, re-visit the NuGet source configuration from step 1).
2) You also need to add framework-specific dependencies, which you can do by updating the ‘Frameworks’ section to look like this (You will not always need to do this, but I think this is a bit of a beta bug at the moment):
Step 3 – Carry on as usual
Now that these building blocks are in place you can use HttpClient in the same way that you always have done.
Here is a little example that you can use as is; it performs a GET request to a live Web API I have running in Azure and will return some product data.
using (var client = new HttpClient())
var baseUri = "http://playapi.azurewebsites.net/api/products";
client.BaseAddress = new Uri(baseUri);
var response = await client.GetAsync(baseUri);
var responseJson = await response.Content.ReadAsStringAsync();
//do something with the response here. Typically use JSON.net to deserialise it and work with it
I hope this saves someone the several annoying hours it cost me to figure this out! 🙂