Tagged: Web API


ASP.Net Web API Versioning

In this tutorial, we are going to see how to support versioning for ASP.Net Web API endpoints. Versioning helps API owners to roll out enhanced functionalities to different customers on a time to time basis without breaking the old versions of endpoints. Usually we achieve versioning in Web API by creating a custom IHttpControllerSelector through which we will route the request to appropriate controller version based on custom header or URL pattern. Thanks to Chris Martinez for creating aspnet-api-versioning nuget, through which versioning was made easy.


Upload large files to Azure Blob Storage through Azure Web Role WebApi services and Azure Cache Service

The main aim of this project is to provide a working scalable solution for uploading large files to server. In this modern era, when web development taking modern cloud workflows, it is time for us to solve the traditional problems which we encounter in uploading files in a more easier and simpler way. We pretty much experience traditional ASP.Net Web problems at the time of working with file uploads which range from 4MB to hundred’s of MBs. Even though ASP.Net allowed some provisions (which would make uploading files easier) by making some Config changes (for example – maxRequestLength and executionTimeOut), still server timeouts and long running connections are creating chaos in this particular requirements.


File Upload using MultipartFormDataStreamProvider in ASP.Net WebAPI

In this short tutorial, we are going to see how to upload multiple files using MultipartFormDataStreamProvider in ASP.Net WebAPI. The concept is based on Multipart/form-data in which we can POST not only multiple file contents but also regular form fields which will be available as NameValueCollection on server side.In this tutorial we also see how to override the default behavior of MultipartFormDataStreamProvider which stores the name in a unique BodyPart_{GUID} format to much more meaningful name. We will also invoke our WebAPI using Fiddler to POST file data. Alongside we develop a sample console application which will POST file data using HttpClient class.


Attribute based routing in ASP.Net Web API

Most of us already knew the traditional routing concepts in Web API which we configure in WebApiConfig file. This approach solves most of the basic routing problems, but it limits the granular control and flexibility over custom routes. Attribute based routing is a popular nuget which offers flexibility in managing routes at Controller/Action level. In this tutorial we are going to see how we can get the basic Attribute based routing works.


Restrict REQUEST Media Types in ASP.Net Web API

In this short tutorial, we are going to see how to restrict different requests for different media types in Web API. In reality, a generic Web API needs to support different Content-types to provide interoperability for different platforms. By default, ASP.Net Web API supports JSON and XML formatters, and we can add additional formatters by implementing MediaTypeFormatter class. But lets take a particular requirement where we need to only support application/json and all other requests should be invalidated. In that case first we have to remove support for all other different formatters in our Web API. Then we need to check HttpRequestMessage for its Accept headers, if that request is made for application/json we are going to serve it or else a HttpExceptionMessage has to be raised.


Simple Custom Media Formatter in ASP.Net Web API

Media formatters in ASP.Net Web API is useful to define our own custom content-type with which we can present data in a specific format. By default Web API support XML, JSON and form-urlencoded data formatters. But with our own media formatter we can have full control on data serialization and deserialization processes. Writing custom media formatters helps us in exposing our logic/data to specific clients with their custom specific formats in place.


Testing ASP.Net Web API–GET, POST, PUT, DELETE using Fiddler

In the recent past I am fortunate to get my hands on to new technology ASP.Net Web API. I was fascinated to hear ASP.Net Web API’s definition from MSDN as follows – “ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices. ASP.NET Web API is an ideal platform for building RESTful applications on the .NET Framework.”. For folks like me who enjoyed working starting with traditional XML Web Services to WCF Services, ASP.Net Web API is a huge bonus in terms of lightweight, MVC coding standards, No huge configuration files and routing. Apart from former said reasons, there are many other like having own media formatters to serve different MIME types, custom exception and action filters, flexible tracing etc. On top of every sensible reason, ASP.Net Web API is easy to code and use.