ASP.NET Web API Samples on Codeplex


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.

Prerequisites

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

or download the samples by selecting the Download link:

Download

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. 

Have fun!

Henrik

Comments (18)

  1. Welcome to the web blog of Janine Patterson says:

    Thanks for another informative site. Where else could I get that type of information written in such an ideal way like this post.This post is really goregious.

    http://janinepatterson.com/

  2. Mel says:

    Hi Henrik,

    Can you please upload samples about how to authenticate using various kinds of authentication (OAuth, FormsAuth, OpenId, WIndows Integration, etc) in ASP.NET Web API? It would really be very helpful. Thanks

  3. Henrik Frystyk Nielsen says:

    Mel, yes, authentication samples is a great idea — will see what we can do.

  4. Neon says:

    Hi Henrik,

    Can you please show an example how to post a collection ie. List<Object> to web api POST using httpclient

  5. Henrik Frystyk Nielsen says:

    Neon, If you look at the JsonUploadSample then it does something very similar — just using a single Contact object. If you just change Contact to List<Contact> and upload a list instead then it will work.

    Is that what you are looking for?

    Henrik

  6. Grreat job.  The samples I have gone through so have given me a good insight into how httpClient works.

    Auth Sample will be great especially using  httpClient.  (There are already many Javacsript samples for WinLive, Google, Facebook)

  7. Matthew says:

    the link or "download the samples by selecting the Download link:", just above the picture is incorrect.  It is going to aspnetwebstack instead of aspnet.

  8. Henrik Frystyk Nielsen says:

    Matthew,

    Thanks for pointing out the broken link — fixed!

    Henrik

  9. Henrik Frystyk Nielsen says:

    SammyD — auth samples are indeed on the list — I am going to the IETF meeting in Vancouver next week so it may take some more days.

    Henrik

  10. Henrik: I'll be happy to Test them for you, if you want.  I already have accounts setup for WinLive, facebook, gmail and twitter

  11. Khuzema says:

    Please provide a how-to-sample for a complex object like a customer order (which requires insert/update to multiple tables). This would be highly appreciated. Thanks

  12. RichM says:

    It would be interesting to see samples of self-hosting the Web API in a Windows service application and also self-hosting using HTTPS and certificates.  This would be very helpful.  Thanks for the samples!

  13. hi

    when i try to buid the solution it gives me the following error

    http://i.imgur.com/24xME.png

  14. Henrik, testing fw 4 WorldBankSample and I get this exception thrown from contentTask: Type 'Newtonsoft.Json.Linq.JToken' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute.  If the type is a collection, consider marking it with the CollectionDataContractAttribute.  See the Microsoft .NET Framework documentation for other supported types.

  15. Henrik Frystyk Nielsen says:

    mgmoody, I suspect you may have old bits. If you try the RTM version then there should be no problems.

  16. Sinedyip says:

    Thank you

  17. Stefanpu says:

    Good day!

    When start one of the "Web API" samples the start page has error: "This operation requires IIS integrated pipeline mode." Does anyone knows what might be the reason. I just download the source code and restore the nuget packages.

    Thanks,

    Stefan

  18. Roger Swetnam says:

    Hi:

    I am trying to run the Web Api Basic Authentication sample (aspnet.codeplex.com/…/latest) – the instructions say:  

    To run the sample, first start the server; then run the client and press ENTER.

    For more information about the samples, please see

    go.microsoft.com/fwlink

    1.  How to I start the server

    2.  Once the server is running, how do i run the sample?

    Thank you.

    Roger