Web API Samples List
Bing Translate Sample | VS 2012 source
Shows how to call the Microsoft Translator service using the HttpClient class. The Microsoft Translator service API requires an OAuth token, which the application obtains by sending a request to the Azure token server for each request to the translator service. The result from the token server is fed into the request sent to the translation service. Before running this sample, you must obtain an application key from Azure Marketplace and fill in the information in the AccessTokenMessageHandler sample class.
Uses HttpClient to download a map of Redmond, WA from Google Maps API, saves it as a local file, and opens the default image viewer.
Shows how to write a simple Twitter client using HttpClient. The sample uses an HttpMessageHandler to insert OAuth authentication information into the outgoing HttpRequestMessage. The result from Twitter is read using JSON.NET. Before running this sample, you must obtain an application key from Twitter, and fill in the information in the OAuthMessageHandler sample class.
Shows how to retrieve data from the World Bank data site, using JSON.NET to parse the result.
Web API Samples
Getting Started with ASP.NET Web API | VS 2012 source
Shows how to create a basic web API that supports HTTP GET requests. Contains the source code for the tutorial Your First ASP.NET Web API.
Shows how to use ASP.NET Web API to build web APIs that support browser clients and can be easily called using jQuery.
Contact Manager | VS 2010 source
This sample uses ASP.NET Web API to build a simple contact manager application. The application consists of a contact manager web API that is used by an ASP.NET MVC application and a Windows Phone application to display and manage a list of contacts.
Shows how to implement HTTP batching within ASP.NET. The batching consists of putting multiple HTTP requests within a single MIME multipart entity body, which is then sent to the server as an HTTP POST. The requests are processed individually, and the responses are put into another MIME multipart entity body, which is returned to the client.
Shows how to read and write request and response entities asynchronously using streams. The sample controller has two actions: a PUT action that reads the request entity body asynchronously and stores it in a local file, and a GET action that returns the contents of the local file.
Custom Assembly Resolver Sample | VS 2012 source
Shows how to modify ASP.NET Web API to support discovery of controllers from a dynamically loaded library assembly. The sample implements a custom IAssembliesResolver which calls the default implementation and then adds the library assembly to the default results.
Shows how to create a custom media type formatter using the BufferedMediaTypeFormatter base class. This base class is intended for formatters which primarily use synchronous read and write operations. In addition to showing the media type formatter, the sample shows how to hook it up by registering it as part of the HttpConfiguration for your application. Note that it is also possible to use the MediaTypeFormatter base class directly, for formatters which primarily use asynchronous read and write operations.
Shows how to customize the parameter binding process, which is the process that determines how information from a request is bound to action parameters. In this sample, the Home controller has four actions:
- BindPrincipal shows how to bind an IPrincipal parameter from a custom generic principal, not from an HTTP GET message;
- BindCustomComplexTypeFromUriOrBody shows how to bind a complex-type parameter, which could come either from the message body or from the request URI of an HTTP POST message;
- BindCustomComplexTypeFromUriWithRenamedProperty shows how to bind a complex-type parameter with a renamed property which comes from the request URI of an HTTP POST message;
- PostMultipleParametersFromBody shows how to bind multiple parameters from the body for a POST message;
Shows how to upload files to an ApiController using MIME Multipart File Upload, and how to set up progress notifications with HttpClient using ProgressNotificationHandler. The controller reads the contents of an HTML file upload asynchronously and writes one or more body parts to a local file. The response contains information about the uploaded file (or files).
This sample is similar to the File Upload Sample, but instead of saving the uploaded files on local disk, it asynchronously uploads the files to Azure Blob Store using Windows Azure SDK for .NET. It also provides a mechanism for listing the blobs currently present in an Azure Blob Storage Container. You can try out the sample running against Azure Storage Emulator that comes with the Azure SDK. If you have an Azure Storage Account, you can run against the real storage service as well.
Shows how to wire up HttpMessageHandler instances on both the client (HttpClient) and server (ASP.NET Web API). In the sample, the same handler is used on both the client and server. While it is rare that the exact same handler would run in both places, the object model is the same on client and server side.
JSON Upload Sample | VS 2012 source
Shows how to upload and download JSON to and from an ApiController. The sample uses a minimal ApiController and accesses it using HttpClient.
Shows how to access multiple remote sites asynchronously from within an ApiController action. Each time the action is hit, the requests are performed asynchronously, so that no threads are blocked.
This sample project creates a Nuget package that will install a custom in-memory trace writer into ASP.NET Web API applications.
Shows how to use MongoDB as the persistent store for an ApiController, using a repository pattern.
Response Body Processor Sample | VS 2012 source
Shows how to copy a response entity (that is, an HTTP response body) to a local file before it is transmitted to the client, and perform additional processing on that file asynchronously. The sample implements an HttpMessageHandler that wraps the response entity with one that both writes itself to the output as normal and to a local file.
Shows how to upload an XDocument to an ApiController using PushStreamContent and HttpClient.
Validation Sample | VS 2010 source
Shows how you can use validation attributes on your models in ASP.NET WebAPI to validate the contents of the HTTP request. Demonstrates how to mark properties as required, how to use both framework-defined and custom validation attributes to annotate your model, and how to return error responses for invalid model states.
Shows an ApiController added to a Web Forms project.
RestBugs is a simple bug tracking application that shows how to use ASP.NET Web API and the new HTTP Client library to create a hypermedia-driven system. The sample includes both client and server implementations, using ASP.NET Web API. The server uses a custom Razor formatter to generate resource representations. The sample also provides a node.js server to illustrate the benefits that come from using a hypermedia design to decouple clients and servers.