Here’s how to get up and running with the ASP.NET Web API and HttpClient samples that you can find in our samples repository on aspnet.codeplex.com. For a list of the samples please see List of ASP.NET Web API and HttpClient Samples as well as our list of samples on www.asp.net.
The samples are split into two parts: those that target .Net 4 and those that target .Net 4.5. The latter use the async/await keywords introduced in Visual Studio 2012 which makes programming with asynchronous tasks much simpler.
Many of the samples use self host for starting ASP.NET Web API (that is, they run outside ASP.NET). These samples all use the local TCP port 50231 to listen for HTTP traffic. In order to start listening on that port you have to register an HTTP namespace with http.sys (or alternatively run Visual Studio as Administrator). You register the namespace from an elevated command prompt like this:
- netsh http add urlacl http://+:50231/ user=%USERDOMAIN%\%USERNAME%
For details, see how to register an http namespace with http.sys.
Getting the Samples
You can either clone the samples repository using the command (see Using Git with CodePlex for details):
- git clone https://git01.codeplex.com/aspnet
The former (i.e. using git) is best if you want to stay up to date with respect to the samples.
Running the Samples
The samples can all be run by hitting F5 in Visual Studio but some may need additional information such as an application key. The first time you run a sample, it will download the various NuGet packages and ask you to confirm their installation for that project.
Note: When downloading the NuGet packages you may see a compilation warning saying something like this:
Package restore is disabled by default. To give consent, open the Visual Studio Options dialog, click on Package Manager node and check ‘Allow NuGet to download missing packages during build.’ You can also give consent by setting the environment variable ‘EnableNuGetPackageRestore’ to ‘true’.
In that case do as the message says and check Allow NuGet to download missing packages during build under Package Manager in Options.
Comments and Suggestions
Please file issues on the samples as well as ideas for other samples. Let us know what you think; what you would like to see, and how this can be a useful collection of samples.