+91-90427 10472
         
Dot net training in Chennai -Maria Academy

C# Web API

05 May 2023

C# Web API

Asp.net Web API is a framework that is provided by Microsoft and used to create HTTP services.

ASP.NET Web API is an easy-to-build HTTP web service. To reach a range of clients, including browsers, mobile applications, desktop applications, and IOTs.

The Asp.net web API has two layers.

 1.client layer

 2.server layer

It is accessed by a wide range of clients, including web browsers, mobile applications, and desktop applications, when the client layer makes a request to the web API to get an output response in different formats like JSON or XML.

Http Request = http://servername/apiname?id=101
Http Response = {“Name”,ABC,”Age”:”54”}

What is HTTP?  
HTTP – Hyper Text Transfer Protocol means the user gets a different device to get a request, but the output gets the same response server, also called HTTP.
HTTP Is used more than providing web page and also strong foundation for the developing API.
HTTP is a essential and pervasive protocol  

Why to choose ASP.NET Web API?

  • Web API is light weight framework for API Development
  • Web API has built-in support for standard formats like JSON and XML., and Other Formats.
  • Web API is a Content Negotiation
  • c#.net Web API maps HTTP Verbs to method names

Features of Web API

Filter: It gives filters to the web application.
Action Result: It can keep the action logos client, such as information recovery.
Routing: It makes a difference in routing through different APIs.
Controller: It gives useful capabilities to the web application.
Model: It gives the basic capabilities of the web application.  

Types of Web API in C#

  1. public
  2. partner
  3. private
  4. Composite

Public APIs 

 A public API is open and available for use by outside developers or businesses; its application, Data, will develop and offer a public API. also called public APIs or external APIs.

Partner APIs

Partner APIs can interface the internal client information system with those outside groups no other APIs are allowed. APIs are generally stronger authentication, authorization, and security machines.

private APIs

Private APIs are used by enterprises to connect system and data APIs for internal use at certain security levels.

Composite APIs

Composite APIs generally combine two or more APIs’ interdependent operations. Composite APIs sometimes improve speed and performance.

C# Web API Sample Programming

Open Visual Studio 2019 > Click Create a new project > Alt+s > ASP.NET Web Application (.NET Framework) > ProjectName  ASP.NET MVC Web API

Create a new Models.cs Application Snapshot as below ,

Models Folder Right click > Add > Click Class > Reference Name Empolyee.cs

Replace the  Empolyee.cs Class with below snippet,

  using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;  

namespace ASP.NET_MVC_Web_API.Models
{    
public class Empolyee    
{        
public int ID { get; set; }        
public string Name { get; set; }        
public DateTime JoiningDate { get; set; }        
public int Age { get; set; }    
}
}

Controller Folder Right click > Add > Click Controller > Click  Web API

> Click Web API 2 Controller Empty> ControllerName EmpolyeeController.cs

 Create a new Controller.cs Application Snapshot as below

Replace the EmpolyeeController.cs With below Snippet ,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using ASP.NET_MVC_Web_API.Models;  

namespace ASP.NET_MVC_Web_API.Controllers
{    
public class EmpolyeeController : ApiController    
{        
Empolyee[] employees = new Empolyee[]
{            
new Empolyee            
{                
ID = 1,                
Name = “sam”,                
JoiningDate = DateTime.Parse(DateTime.Today.ToString()),                
Age = 29            
},            
new Empolyee            
{                
ID = 2,                
Name = “Balu”,                
JoiningDate = DateTime.Parse(DateTime.Today.ToString()),                 
Age = 36            
},            
new Empolyee            
{                
ID = 3,                
Name = “Michel”,                
JoiningDate = DateTime.Parse(DateTime.Today.ToString()),                
Age = 22            
}        
};        
public IEnumerable<Empolyee> GetAllEmployees()        
{            
return employees;        
}        
public IHttpActionResult GetEmployee(int id)        
{            
var employee = employees.FirstOrDefault((p) => p.ID == id);            
if (employee == null)            
{                
return NotFound();            
}            
return Ok(employee);        
}    
}
}  

Run the Application and Replace the Url

Output :