WEB API
WEB API
WEB API
API:
Communicate between two parties like two applicatioins.
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.
.Net Framework:
.Net Core:
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
We have to set disable the Cors for specific action method or controller
like [DisableCors]
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
{
}
}
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.
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
[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
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");
});
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.