Getting Started With ASP.Net Web API

ASP.NET Web API

The 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. (Reference from the MSDN.)

SOAP

The Simple Object Access Protocol (SOAP) is a XML based communication protocol for exchanging information over HTTP. It is platform independent as well as language independent.

REST

REST is a representable state transfer and it uses standard Hypertext Transfer Protocol (HTTP) actions for CRUD operations, like GET, POST, PUT and DELETE. It supports any text format including XML.

HTTP Methods

The following are the HTTP methods verbs:

  1. GET: Read (or) Retrieve
  2. POST: Create
  3. PUT: Update
  4. DELETE: Delete

S. No

Methods

Description

1

GET

Retrieves a representation of a resource.

2

POST

Creates a resource.

3

PUT

Updates a representation of the original resource.

4

DELETE

Deletes a resource.

 
RMM
 
 
 
 

Procedure

1. Open Visual Studio.

 
 
2. Go to File menu and select new, then Project click.
 
 

3. You can select an installed template under the Visual C# and select Web option. Then select ASP.NET Web Application. Then enter the application name and location then click the OK button.


 
4. You can select Empty template and check the Web API checkbox. Then click the OK button.
 
 
 
5. You can then see the Solutions Explorer as in the following:
 
 
 
6. Go to the Solutions Explorer and right-click the Models Folder then select Add into adding model in your project.
 
 
 
7. Now, you can select class and enter the name of the class “Employee” then click the Add button.
 
 
 
Adding Model
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5.   
  6. namespace EmployeeApps.Models  
  7. {  
  8.     public class Employee  
  9.     {  
  10.         public int EmployeeId { getset; }  
  11.         public string EmployeeName { getset; }  
  12.         public string EmployeeDepartment { getset; }  
  13.         public decimal EmployeeSalary { getset; }  
  14.         public string EmployeeLocation { getset; }  
  15.     }  
  16. }  
8. Go to the Solutions Explorer and right-click the Controller Folder then select Add into adding Controller in your project.
 
 
 
9. Now, you can select Web API2 Controller – Empty and click the Add button.
 
 
 
10. You can then enter the controller name “EmployeeController” and click the Add button.
 
 
 
11. You can then see the Solutions Explorer as in the following:
 

 
Adding Controller
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Net.Http;  
  6. using System.Web.Http;  
  7. using EmployeeApps.Models;  
  8.   
  9. namespace EmployeeApps.Controllers  
  10. {  
  11.     public class EmployeeController : ApiController  
  12.     {  
  13.         Employee[] employee = new Employee[]  
  14.         {  
  15.             new Employee{ EmployeeId=100, EmployeeName="Santhakumar", EmployeeDepartment="DOTNET", EmployeeSalary=10000.00m, EmployeeLocation="Chennai"},  
  16.             new Employee{ EmployeeId=101, EmployeeName="Moorty", EmployeeDepartment="DOTNET", EmployeeSalary=30000.00m, EmployeeLocation="Chennai"},  
  17.             new Employee{ EmployeeId=102, EmployeeName="Rajkumar", EmployeeDepartment="DOTNET", EmployeeSalary=10000.00m, EmployeeLocation="Chennai"},  
  18.             new Employee{ EmployeeId=103, EmployeeName="Saktikumar", EmployeeDepartment="DOTNET", EmployeeSalary=50000.00m, EmployeeLocation="Chennai"},  
  19.             new Employee{ EmployeeId=104, EmployeeName="Sathikumar", EmployeeDepartment="DOTNET", EmployeeSalary=10000.00m, EmployeeLocation="Chennai"},  
  20.             new Employee{ EmployeeId=105, EmployeeName="Praveen", EmployeeDepartment="DOTNET", EmployeeSalary=20000.00m, EmployeeLocation="Chennai"},  
  21.   
  22.         };  
  23.   
  24.         public IEnumerable<Employee> GetAllEmployeeDetails()  
  25.         {  
  26.             return employee;  
  27.         }  
  28.   
  29.     }  

WebApiConfig
  1. public static void Register(HttpConfiguration config)    
  2. {    
  3.      // Web API configuration and services    
  4.      // Web API routes    
  5.      config.MapHttpAttributeRoutes();    
  6.     
  7.      config.Routes.MapHttpRoute(    
  8.        name: "DefaultApi",    
  9.            routeTemplate: "api/{controller}/{id}",    
  10.            defaults: new { id = RouteParameter.Optional }    
  11.      );    
  12.  }  
Output


Conclusion

This article helps you to understand the ASP.NET Web API and its implementation in Visual Studio 2013.


Similar Articles