I have explained below the What, Why, How and Where part of Web API.
What:
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.
Web API's controller action only return data that is serialized and sent to the client.
Why:
To expose your data to all of the modern devices from your rich internet applications. Typically, these applications use their HTTP connection to return data in the form of JSON or XML rather than HTML markup.
Content Negotiation based on Accept headers for request and response serialization.
How to use:
Create a controller class which should inherit the "ApiController" base class from System.Web.Http. Your controller's methods can use any of those GET, PUT, POST, DELETE HTTP verbs to communicate with the clients.
Eg.
Controller:
public class ProductController : ApiController
{
[HttpGet]
public HttpResponseMessage ValidateProduct(int productId)
{
// validation logic goes here..
}
}
You can call your Web API method from client-side as below,
var apiUrl = "/api/Product/ValidateUpdate?productId=" + 10;
$.ajax({
url: apiUrl,
type: "GET",
contentType: "application/json;charset=utf-8",
success: function (data) { // After successfull call, things to be done goes here..
},
error: function (data) { // In case of exception, handle & display message...
}
});
Where:
You could use Web API in your ASP.NET Web Forms or MVC Applications or Single Page Applications.
Hosting:
It can be hosted with in the application or on IIS.