Sdílet prostřednictvím


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 https://+: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):

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

  • Anonymous
    July 24, 2012
    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/

  • Anonymous
    July 25, 2012
    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

  • Anonymous
    July 26, 2012
    Mel, yes, authentication samples is a great idea -- will see what we can do.

  • Anonymous
    July 26, 2012
    Hi Henrik, Can you please show an example how to post a collection ie. List<Object> to web api POST using httpclient

  • Anonymous
    July 27, 2012
    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

  • Anonymous
    July 28, 2012
    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)

  • Anonymous
    July 28, 2012
    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.

  • Anonymous
    July 29, 2012
    Matthew, Thanks for pointing out the broken link -- fixed! Henrik

  • Anonymous
    July 29, 2012
    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

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

  • Anonymous
    July 31, 2012
    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

  • Anonymous
    August 08, 2012
    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!

  • Anonymous
    August 12, 2012
    hi when i try to buid the solution it gives me the following error http://i.imgur.com/24xME.png

  • Anonymous
    August 13, 2012
    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.

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

  • Anonymous
    December 13, 2012
    Thank you

  • Anonymous
    April 03, 2014
    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

  • Anonymous
    April 12, 2014
    The comment has been removed