Introduction to Web API
In this article we are going to focus on what is ASP.NET Web API(Application Programming Interface) and why it is needed. The Prerequisites required to learn Web API. Tools to test a Web API service.
WEB API is used to create HTTP services.
In this article we are going to focus on what is ASP.NET Web API and how to use it. The Prerequisites to learn Web API.
What is ASP.NET Web API
It is a framework for creating web API s on top of .NET Framework. It makes creating HTTP service simple and easy and supports wide range of devices.
Why Web APIs
1.Using Web API, We can not only expose data but also create our own services
2. We can use it for a wide range of clients may be a Browser application or a Mobile Device or any Desktop applications.
3. Makes sending/receiving data over HTTP to a wide range of devices very easy.
4. The client can be a service from other language like ruby or python any xaml application or a JS client.
5.Supports data formats such as JSON / XML and allows clients to provide the required response data format. Provision to provide our own data formatting using MediaTypeFormatters.
6.Simple and clean urls to understand such as : http://dotnetspider.com/session/webapi
7.Since it is based on http protocol it also supports caching.
8.Easy to consume from Javascript.
9.It can be hosted in IIS or any exe file or asp.net or unit test project.
10. It uses routing similar to ASP.NET MVC.
Basic components of Web API
1.Controller:
It is the heart of the Web API application.
It exposes the actions or the resources that can be consumed by the client.
2.Handlers:
IT allows us to keep track of the incoming request or the outgoing response
Facilitate Authentication.
3.Filters:
To provide additional functionality on top of the basic functionality.
Installation Instructions:
It is already included with ASP.NET MVC 4.
Visual Studio 2010
Download and install sp1 for Visual Studio 2010.
Download and install MVC 4 Project from Microsoft site here
http://www.microsoft.com/en-ca/download/details.aspx?id=30683
Visual Studio 2012
Already Built into it.
Prerequisites
Good to have knowledge in MVC architecture but not mandatory.
Good to have knowledge in WCF data services but not mandatory.
C# knowledge is required.
Knowledge in JSON/XML.
How to test Web API services
Fiddler is a great tool to test Web API services. It also provides the facility to tweak the request headers including the content format we expect from service.
Web API in action
Now we will see how to create a simple Web API application using Visual Studio 2012.
Here we will create a Web API that returns a list of students.
Step1: Launch Visual Studio 2012 -> File -> New -> Project -> On the Left Side of open dialog box Select Installed ->Templates -> Visual C# -> Web -> Then select ASP.NET MVC4 Web Application. Name it as "MyFirstWebAPIApp".
Step2: In the New ASP.NET MVC 4 Project window select Empty as shown below and click on ok:
Step3: Right click Models Folder and Add new class "Student".
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MyFirstWebAPIApp.Models
{
public class Student
{
public Student(){}
public Student(int id,string name,DateTime joiningDate,string cls)
{
this.StudentId = id;
this.StudentName = name;
this.JoiningDate = joiningDate;
this.Class = cls;
}
public int StudentId { get; set; }
public string StudentName { get; set; }
public DateTime JoiningDate { get; set; }
public string Class { get; set; }
}
}
Step 4:Right click Controllers folder and add a new class "StudentController" as shown below. This controller handles HTTP requests from the client.
namespace MyFirstWebAPIApp.Controllers
{
public class StudentController : ApiController
{
public List
{
List
lst.Add(new Student(1, "john", new DateTime(2014, 1, 1), "B.Tech I year"));
lst.Add(new Student(1, "robert", new DateTime(2014, 2, 1), "B.Tech I year"));
lst.Add(new Student(1, "tina", new DateTime(2014, 1, 4), "B.Tech I year"));
return lst;
}
}
}
Please note that the controller class inherits from the ApiController class which defines the methods and properties for the web api controller.
The configuration settings for the Web API are stored in the GlobalConfiguration object which inturn contains the Httpconfiguration object.
In the App_Start folder there is a WebApiConfig class which is used for Web API configuration.
In the Global.asax file, In the Application_Start event,Register method of the WebApiConfig class is used to configure the Web API. It is used to add the default WebAPI route.
In the register method, by default it contains the code to route the requests to appropriate controller.
Step 5:Build the project and Press Ctrl +F5 to run the project.
Now the browser window opens. Copy the url.
Step 6:Launch the Fiddler tool. If Fiddler is not already installed. Download and install it from this url
http://www.telerik.com/download/fiddler
In the Fiddler Web Debugger window, click on Composer and paste the url(http://localhost:6329/api/Student) as shown below. The port number may vary based on the availability of the port.
Step 7: Click on the execute tab. Once the request is executed and response is received. Double click on the left hand side window which says URL:/api/Student. This displays the result in JSON format as shown below:
Step 8:To view the output in xml format, We need to update the accept header saying we want the result in xml format by specifying:Accept: application/xml in the Request Header as shown below:
Now click on execute button again and see that the result is now displayed in xml format as shown below: