ASP.NET Web API - Coding Sharp

ASP.NET Web API

Asp.Net Web API is a framework for building HTTP services that can be consumed by a broad range of clients including web browsers, mobile phones and tablets. Actually Web API is not a part of Asp.net MVC Framework, however it is very similar to Asp.Net MVC Framework, It provides similar feature like controller, action results, routing, model binders, filters, dependency injection or Inversion of Control container. It is a part of standard Asp.Net Framework so it can be used with Asp.Net Web Forms as well as Asp.Net MVC. It can be used as a stand alone application like web services. Asp.Net Web API is a very good platform to build a RESTful application.

Nova days web application is not capable to reach more customers, as people using smart phones and tablets in their daily routine life for making their life easy. In short we can say people are moving from web to mobile devices.

So if you want to delivered data fast and in a simple manner for web browsers and smart devices then Web API is a good choice for that because it is compatible for those devices and browsers.

See the below image for the reference

Cool features of Web API

  1. ASP.NET Web API is Very good platform for building RESTful services.
  2. ASP.NET Web API maps HTTP verbs to method names.
  3. Responses have an Accept header and HTTP status code.
  4. ASP.NET Web API is built on top of ASP.NET.
  5. Asp.Net Web API supports ASP.NET request/response pipeline
  6. Asp.Net Web API supports convention-based CRUD Actions since it works with HTTP verbs GET, POST, PUT and DELETE.
  7. Responses are formatted by Web API’s MediaTypeFormatter into JSON, XML or whatever format you want to add as a MediaTypeFormatter.
  8. ASP.NET Web API supports different formats of response data. Built-in support for JSON, XML format.
  9. ASP.NET Web API can be hosted in IIS, Self-hosted or other web server that supports .NET 4.0+.
  10. It may accepts and generates the content which may not be object oriented like images, PDF files etc.
  11. ASP.NET Web API also supports for Odata. So if we add a new [Queryable] attribute on a controller action method which returns Iqueryable, clients can use the method for Odata query composition.
  12. Asp.Net also supports the MVC feature such as controller, routing, filters, action results, model binder, IOC container/dependency injection that makes it more simple and robust.

Why we choose Web API?

  1. First of all it is an open source.
  2. It only based on HTTP and easy to define, easy to expose and easy to consume in a RESTful way.
  3. It is very good for those devices which has very limited bandwidth like smart phone, tablets etc, Because it is very light weight architecture.
  4. It does not have any complex and extensive configuration like WCF services.
  5. Asp.Net Web API is the best choice when we don’t need SOAP and only need a web services.
  6. Asp.Net Web API is used to build simple non-SOAP base HTTP services on top of the already build WCF message pipeline.

When to choose Asp.Net Web API or Web API?

  1. If you are using .Net framework greater then or 4.0
  2. If we want to develop services which supports only HTTP protocol.
  3. If you are familiar with Asp.Net MVC then go for Web API.
  4. If you want to build RESTful HTTP based services then go for Web API

When to choose WCF services

  1. If you want to use Request-Reply, duplex message exchange patterns or one way then choose WCF services.
  2. If you want to use .Net framework below 4.0
  3. If you want to build a services with WS-* standards like Reliable messaging, Message Security or Transaction then go for WCF services.
  4. If you want to develop a services which not supports only HTTP and supports TCP and Named pipe then go for WCF services.

Difference between WCF and Web API

For the WCF and Web API Difference please refer my previous article.

Web API Version

API Version Supported Framework As.Net MVC Version Visual Studio Version
Web API 1.0 .Net Framework 4 Asp.Net MVC 4 Visual Studio 2010
Web API 2 .Net Framework 4.5 Asp.Net MVC 5 Visual Studio 2012Visual Studio 2013

I think you have now clear picture about Web API. Please share your valuable feedback here  in comments.

In our next article we will see the Web API in real action with small example with Visual Studio.