Quantcast
Channel: ASP.NET Blog
Viewing all articles
Browse latest Browse all 7144

List of ASP.NET Web API and HttpClient Samples

$
0
0

Here is a list of the Web API and HttpClient samples you can find in our samples repository on aspnet.codeplex.com. They illustrate various features of Web API and HttpClient targeting either Visual Studio 2010 using .NET 4 or Visual Studio 2012 using .NET 4.5 with async/await language support.

For details on how get up and running with the samples, please see the blog ASP.NET Web API Samples on Codeplex. You can also check out additional information about ASP.NET Web API as well as find the Open Source runtime on Codeplex.

If there are samples that you miss or you find issues then please register an issue and let us know what you think!

HttpClient Samples

Bing Translate Sample | VS 2012 source

Sample illustrating using the Bing translator API from HttpClient. The API requires an OAuth token which we obtain by sending a request to the Azure token server each time we send a request to the translator service. The result from that request is fed into the request sent to the translation service itself. Before you can run this sample you must obtain an application key from Azure Marketplace and fill in the information in the AccessTokenMessageHandler sample class.

Google Maps Sample | detailed description | VS 2012 source

This sample 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.

Twitter Client Sample | detailed description | VS 2012 source

This sample illustrates how to write a simple twitter client using HttpClient. The sample uses an HttpMessageHandler to insert the appropriate OAuth authentication information into the outgoing HttpRequestMessage. The result from twitter is read using JSON.NET as a JToken. Before you can run this sample you must obtain an application key from twitter, and fill in the information in the OAuthMessageHandler sample class.

World Bank Sample | detailed description | VS 2010 source | VS 2012 source

This sample shows how to retrieve data from the World Bank data site using JSON.NET to parse the result as JToken.

Web API Samples

Batching Sample | detailed description | VS 2012 source

This sample 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 then processed individually and the responses are put into another MIME multipart entity body which is returned to the client.

Content Controller Sample | detailed description | VS 2010 source | VS 2012 source

This ASP.NET Web API sample illustrates how to read and write request and response entities asynchronously using streams. The sample controller has two actions: a PUT action which reads the request entity body asynchronously and stores it in a local file and a GET action which returns the content of the local file.

Custom Assembly Resolver Sample | VS 2012 source

This sample illustrates how to modify ASP.NET Web API to support discovery of controllers loaded dynamically from a dynamically loaded controller library assembly. The sample implements a custom IAssembliesResolver which takes the default implementation providing the default list of assemblies and then adds the ControllerLibrary assembly as well.

Custom Media Type Formatter Sample | detailed description | VS 2010 source

This sample illustrates how to create a custom media type formatter using the BufferedMediaTypeFormatter base class for formatters which primarily are using 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.

Custom Parameter Binding Sample | detailed description | VS 2010 source

This ASP.NET Web API sample illustrates 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:

  1. BindPrincipal shows how to bind an IPrincipal parameter from a custom generic principal, not from an HTTP GET message;
  2. BindCustomComplexTypeFromUriOrBody shows how to bind a Complex Type parameter which could come from either the message body or request Uri of an HTTP POST message;
  3. BindCustomComplexTypeFromUriWithRenamedProperty shows how to bind a Complex Type parameter with a renamed property which comes from request Uri of an HTTP POST message;
  4. PostMultipleParametersFromBody shows how to bind multiple parameters from body for a POST message;

File Upload Sample | detailed description | VS 2012 source

This sample illustrates how to upload files to an ApiController using HttpClient using MIME Multipart File Upload as defined by HTML. It also shows how to set up progress notifications with HttpClient using ProgressNotificationHandler. The FileUploadController reads the contents of an HTML file upload asynchronously and writes one or more body parts to a local file. It then responds with a result containing information about the uploaded file (or files).

Http Message Handler Pipeline Sample | detailed description | VS 2010 source

This sample illustrates how to wire up HttpMessageHandlers on both client and server side as part of either HttpClient or ASP.NET Web API. In the sample, the same handler is used on both client and server side. While it is rare that the exact same handler can run in both places, the object model is the same on client and server side.

JSON Upload Sample | VS 2012 source

This sample illustrates how to upload and download JSON to and from an ApiController. The sample uses a minimal ApiController and accesses it using HttpClient.

Mashup Sample | detailed description | VS 2012 source

This sample shows how to asynchronously access multiple remote sites from within an ApiController action. Each time the action is hit, the requests are performed asynchronously so that no threads are blocked.

Memory Tracing Sample | detailed description | VS 2010 source

This sample illustrates how to wire up HttpMessageHandlers on both client and server side as part of either HttpClient or ASP.NET Web API. In the sample, the same handler is used on both client and server side. While it is rare that the exact same handler can run in both places, the object model is the same on client and server side.

MongoDB Sample | detailed description | VS 2012 source

This sample illustrates how to use MongoDB as the persistent store for an ApiController using a repository pattern.

Response Body Processor Sample | VS 2012 source

This sample illustrates how to copy a response entity (i.e. an HTTP response body) to a local file before it is transmitted to the client and perform additional processing on that file asynchronously. It does so by hooking in a HttpMessageHandler that wraps the response entity with one that both writes itself to the output as normal and to a local file.

Upload XDocument Sample | detailed description | VS 2012 source

This sample illustrates uploading an XDocument to an ApiController using PushStreamContent and HttpClient.

Validation Sample | VS 2010 source

This sample illustrates how you can use validation attributes on your models in ASP.NET WebAPI to validate the contents of the HTTP request. It 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.

Web Form Sample | detailed description | VS 2010 source

This sample shows an ApiController added to a WebForm project using the menu "Add New Item" and then select "Web API Controller Class". In addition to the controller we also add a default route to global.asax.cs file.

Web API Extensions Preview Samples

OData Queryable Sample | detailed description | VS 2010 source

This sample shows how to introduce OData queries in ASP.NET Web API using either the [Queryable] attribute or by using the ODataQueryOptions action parameter which allows the action to manually inspect the query before it is being executed.

The CustomerController shows using [Queryable] attribute and the OrderController shows how to use the ODataQueryOptions parameter. The ResponseController is similar to the CustomerController but instead of the GET action returning IEnumerable<Customer> it returns an HttpResponseMessage. This allows us to add extra header fields, manipulate the status code, etc. while still using query functionality. The sample illustrates queries using $orderby, $skip, $top, any(), all(), and $filter.

OData Service Sample | detailed description | VS 2010 source

This sample illustrates how to create an OData service consisting of three entities and three ApiControllers. The controllers provide various levels of functionality in terms of the OData functionality they expose:

The SupplierController exposes a subset of functionality including Query, Get by Key and Create, by handling these requests:

  • GET /Suppliers
  • GET /Suppliers(key)
  • GET /Suppliers?$filter=..&$orderby=..&$top=..&$skip=..
  • POST /Suppliers

The ProductsController exposes GET, PUT, POST, DELETE, and PATCH by implementing an action for each of these operations directly.

The ProductFamilesController leverages the EntitySetController base class which exposes a useful pattern for implementing a rich OData service.

In addition the OData service exposes a $metadata document which allows the data to the consumed by WCF Data Service clients and other clients that accept the $metadata format.


Have fun!

Henrik


Viewing all articles
Browse latest Browse all 7144

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>