What is a Web API?
A Web API is an extensible framework for building HTTP based services that can be accessed in different applications on different platforms such as web, windows, mobile, etc. It is somewhat the same as an ASP.NET MVC web application, except that it sends data as a response instead of an html view.
- Ideal platform for building RESTful services
- Can be hosted in IIS, Self-hosted or other web server that supports .NET 4.0+.
- It maps HTTP verbs to method names
- It is built on top of ASP.NET and supports ASP.NET request/response pipeline
- It supports different formats of response data. Built-in support for JSON, XML, BSON format.
- Open source and ships with .NET framework.
- Does not support Reliable Messaging and transaction.
- Web API 1.0 (Supported .NET Framework 4.0)
- Web API 2.0 (Supported .NET Framework 4.5)
Features of Web API 2.0
- Attribute routing: With attribute routing, we can now have both HTTP VERB and action based routing on the same controller. We can achieve RESTful hierarchy (nested routing) on a single controller without any problems.
- OWIN integration / Katana Project: Web API 2 includes a self host package, calledAspNet.WebApi.OwinSelfHost. It allows you to host Web API using Katana
- IhttpActionResult: Allows the ability to return HttpResponseMessagefrom controllers – which improves code reusability and testability.
- CORS: Implementation of the Cross Origin Resource Sharing specification to Web API developers.
- Filter Overrides: Used to override widely scoped filters (i.e. global level or controller level), from a narrower scope (i.e. action level).
- Testability: Simple and easy to use.
- For more information: https://docs.microsoft.com/en-us/aspnet/web-api/overview/releases/whats-new-in-aspnet-web-api-22
Testing a Web API
It is very easy to test a Web API locally, to check request and response during development. There are third party tools available for testing Web API – for example Fiddler and Postman. They are easy to use, and just need to be downloaded and installed.
Web API Controller
Web API Controller is similar to ASP.NET MVC controller. It handles incoming HTTP requests and send responses back to the caller. The name of a controller class must end with “Controller” and it must be derived from System.Web.Http.ApiController class.
All the public methods of the controller are called action methods. Action method name can be the same as an HTTP verb name or it can start with an HTTP verb with any suffix (case in-sensitive) or you can apply HTTP verb attributes to method.
Action Method Return Type
The Web API action method can have the following return types.
- Primitive or Complex Type
- IHttpActionResult: An action method in Web API 2 can return an implementation of IHttpActionResult class which is more or less similar to ActionResult class in ASP.NET MVC.
Web API handles JSON and XML formats based on Accept and Content-Type headers and it handles these different formats using Media-Type formatters.
These are simply classes responsible for serializing request/response data so that Web API can understand the request data format and send data in the format which the client expects.
Below are the built-in media type formatters:
- JsonMediaTypeFormatter: Handles json format
- XmlMediaTypeFormatter: Handles xml format
- FormUrlEncodedMediaTypeFormatter: Handles HTML form URL-encoded data
- JQueryMvcFormUrlEncodedFormatter: Handles model-bound HTML form URL-encoded data
Web API Filters
Web API includes filters to add extra logic before or after an action method executes. Filters can be used to provide cross-cutting features such as logging, exception handling, performance measurement, authentication and authorization.
Web API Hosting
Web API application can be hosted in two ways:
- IIS Hosting: Web API can be hosted under IIS, in the same way as a web application
- Self Hosting: host a Web API as a separate process other than ASP.NET. It means you can host a Web API in console application or windows service or OWIN or any other process that is managed by .NET framework.