WEB API

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

WEB API

API:
Communicate between two parties like two applicatioins.

HTTP:Hyper Text transport Protocal


It is set of rules and condition.Transfer the content over the internet. And it is stateless i
mean it is connection less protocol like server doesn't maintain the object.And it consider
every request as a new request. It very user full for MIME type
Web API:
1.Open souce framework
2.HTTP protocol
3.Any technology we can make a web Api request
4.Communicate over the networks
5.Content Negotiation
6.Lightweight Framework to make a API development
Eg:Google API,Weather API

Rest:
1.Representational State Transfer
2.It is basically architectural style of development having some principle
3.It does not have a session
4.It does not have a inbuilt encryption
5.Toperform the CRUD operation over the HTTP
6.It will return Json,XML,atom,OData,Media type
7.It is a stateless(Every request in complete happens isolation)
8.Access the all resouce from server using only URI
RestFull:
six architecture constraints imposed by rest for restull webServices.
1.Uniform Interface---->Resouce based
2.client-server--->Independent Clinet and server applicaation
3.Statless--->It does not maintain the client request object
4.cachable---->Reusable like client will make a request to catch
5.Layered system --->security level like Client doesn't know whether server A or server B
like API's are Server A and Data's are Server B
6.Code on demand Optional---> returns the executable code like rendring code

Web API is generally used as a service like information will be shared from the server.

using Microsoft.AspNetCore.Mvc
All the controller class are inherited by ApiController.

sytem.web.http namespace
Every controller should inherits from the ApiController class
HTTP verbs like GET, PUT, POST,PATCH and DELETE

Status codes:
200/OK
201/Created New Item
204/No Content
500/Internal Server Error
404/Not Found
401/Unauthorized
CORS-->Cross Origin Resource Sharing:
Cross Domain communication.

we should enable the CORS.


Microsoft.AspNet.WebApi.Cors ---->NuGet Package

.Net Framework:

EnableCorsAttribute cors = new EnableCorsAttribute("*", "*", "*");


config.EnableCors(cors);

.Net Core:

Add the services and use the middleware like usecors(Policyanme)

Parameters of EnableCorsAttribute:
1.Origins
2.Headers
3.Methods

All Region:
services.AddCors(options =>
{
options.AddPolicy("AllowAllOrigins",
builder => builder.AllowAnyOrigin());
});

Specific Region:
options.AddPolicy("AllowSpecificOrigin",
builder => builder.WithOrigins("http://localhost:1000",
"http://localhost:200").AllowAnyHeader()
.AllowAnyMethod(););

In that time i mean instnace creation we have to pass the some values for the constructor.
First * denoted like request comming from any origion i mean
www.rrd.com,www.v.org,http://test:1453
Second * denoted like content type like header format
Third * denoted like accept all the method like HTTP verbs

It is also enable for action method,Controller and global.

We have to set disable the Cors for specific action method or controller
like [DisableCors]

Json.NET library is generally used by Web API for JSON serialization.

Routing:
1.Conversitional Based---->Its a Default
2.Attributed Based (We need to call the method like config.MapHttpAttributeRoutes() in
the WebApiConfig static class files)

This will apply for both Controller level and action method level
Controller Level:
[Route("students")]
public class TestController : ApiController
{

Action Method Level:


[Route("students")]
public class TestController : ApiController
{
[HttpGet]
[Route("EmployeeList")]
public Student GetEmployee()
{

}
}

Route Constraints:
[Route("{Employee::int}")]
[Route("{Employee::alpha}")]
Min,Max,Range,legth,default value assign

Parameter Binding:
1.FromUri
2.FromBody

Content Negotiation:
1.content type---Request tpe
2.Accept type----Response type

Versioning:
1.It is a technique
2.Different clients can get different implementations of the same Controller based on the
request

How to implement add new features with the existing api. The new concept is called
versioning. As i said earlier already API's are consumed by many clients. so don't affect
the those client and new client. We can't say to client like modified api consumed.we don't
have to compromise the existing client. To overcome this problem we are go to concept of
versioning.

This achieve three ways like


1.Query string
2.URL
3.HTTP Header

Nuget Package Manager------->Microsoft.AspNetCore.Mvc.Versioning


We have to add the below services in the ConfigureServices method of startup.cs file
services.AddApiVersioning(x =>
{
x.DefaultApiVersion = new ApiVersion(1, 0);
x.AssumeDefaultVersionWhenUnspecified = true;
x.ReportApiVersions = true;
});

Query String:
[ApiController]
[ApiVersion("1.0")]
[Route("api/employee")]
public class EmployeeV1Controller : ControllerBase
{
[HttpGet]
public IActionResult Get()
{
return new OkObjectResult("employees from v1 controller");
}
}

[ApiController]
[ApiVersion("2.0")]
[Route("api/employee")]
public class EmployeeV2Controller : ControllerBase
{
[HttpGet]
public IActionResult Get()
{
return new OkObjectResult("employees from v2 controller");
}
}

https://localhost:44381/api/employee?api-version=1.0
https://localhost:44381/api/employee?api-version=2.0

URL based Versioning:


[ApiController]
[ApiVersion("1.0")]
[Route("api/{v:apiVersion}/employee")]
public class EmployeeV1Controller : ControllerBase
{
[HttpGet]
public IActionResult Get()
{
return new OkObjectResult("employees from v1 controller");
}
}

[ApiController]
[ApiVersion("2.0")]
[Route("api/{v:apiVersion}/employee")]
public class EmployeeV2Controller : ControllerBase
{
[HttpGet]
public IActionResult Get()
{
return new OkObjectResult("employees from v2 controller");
}
}

https://localhost:44381/api/1.0/employee
https://localhost:44381/api/2.0/employee

HTTP Header-Based Versioning:

We have to add one more property like ApiVersionReader and assign the
HeaderApiVersionReader object. I mean the versions are passing by Header.
services.AddApiVersioning(x =>
{
x.DefaultApiVersion = new ApiVersion(1, 0);
x.AssumeDefaultVersionWhenUnspecified = true;
x.ReportApiVersions = true;
x.ApiVersionReader = new HeaderApiVersionReader("x-api-version");
});

I am implement by AJAX CALL:


$.ajax({
url: 'www.RRD.com/Employee',
type: 'POST',
dataType: 'json',
data:{"Name":"RRD"},
Accept: application/json; version=1
success: function (data) {
console.log(data);
},
error: function (errorThrown) {
console.log('Error in Operation');
}
});

We can also use different type of version for one Http request.
ApiVersionReader.Combine(new HeaderApiVersionReader("x-api-version"), new
QueryStringApiVersionReader("api-version"));

API Gateway
Filters
Token Based Authentication like JWT(Json Web Token).OAuth i don't know. so i will
explain JWT only
Those concepts are will explain to other document. Because this concept fully on coding
level.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy