Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Getting Started With ASP.Net Web API

DZone's Guide to

Getting Started With ASP.Net Web API

·
Free Resource

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.


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
01. usingSystem; 
02. usingSystem.Collections.Generic; 
03. usingSystem.Linq; 
04. usingSystem.Web; 
05.    
06. namespaceEmployeeApps.Models 
07.
08.     publicclassEmployee 
09.     
10.         publicintEmployeeId { get; set; } 
11.         publicstringEmployeeName { get; set; } 
12.         publicstringEmployeeDepartment { get; set; } 
13.         publicdecimalEmployeeSalary { get; set; } 
14.         publicstringEmployeeLocation { get; set; } 
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
01. usingSystem; 
02. usingSystem.Collections.Generic; 
03. usingSystem.Linq; 
04. usingSystem.Net; 
05. usingSystem.Net.Http; 
06. usingSystem.Web.Http; 
07. usingEmployeeApps.Models; 
08.    
09. namespaceEmployeeApps.Controllers 
10.
11.     publicclassEmployeeController : ApiController 
12.     
13.         Employee[] employee = newEmployee[] 
14.         
15.             newEmployee{ EmployeeId=100, EmployeeName="Santhakumar", EmployeeDepartment="DOTNET", EmployeeSalary=10000.00m, EmployeeLocation="Chennai"}, 
16.             newEmployee{ EmployeeId=101, EmployeeName="Moorty", EmployeeDepartment="DOTNET", EmployeeSalary=30000.00m, EmployeeLocation="Chennai"}, 
17.             newEmployee{ EmployeeId=102, EmployeeName="Rajkumar", EmployeeDepartment="DOTNET", EmployeeSalary=10000.00m, EmployeeLocation="Chennai"}, 
18.             newEmployee{ EmployeeId=103, EmployeeName="Saktikumar", EmployeeDepartment="DOTNET", EmployeeSalary=50000.00m, EmployeeLocation="Chennai"}, 
19.             newEmployee{ EmployeeId=104, EmployeeName="Sathikumar", EmployeeDepartment="DOTNET", EmployeeSalary=10000.00m, EmployeeLocation="Chennai"}, 
20.             newEmployee{ EmployeeId=105, EmployeeName="Praveen", EmployeeDepartment="DOTNET", EmployeeSalary=20000.00m, EmployeeLocation="Chennai"}, 
21.    
22.         }; 
23.    
24.         publicIEnumerable<Employee> GetAllEmployeeDetails() 
25.         
26.             returnemployee; 
27.         
28.    
29.     
30. }
WebApiConfig
01. publicstaticvoidRegister(HttpConfiguration config)   
02. {   
03.      // Web API configuration and services   
04.      // Web API routes   
05.      config.MapHttpAttributeRoutes();   
06.      
07.      config.Routes.MapHttpRoute(   
08.        name: "DefaultApi",   
09.            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.

Topics:
soap ,asp.net ,rest ,asp.net web api ,rmm

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}