Introduction to Web API Recap
Introduction to Web API Recap
ASP.NET Core Web API is a component (part) of ASP.NET Core, which is used create
HTTP-based RESTful services (also known as HTTP services) that can be consumed
(invoked) by wide range of client applications such as single-page web applications, mobile
applications etc.
Asp.Net Core:
Controller
[ApiController]
class ClassNameController
{
//action methods here
}
Optional:
● Is a public class.
● Inherited from Microsoft.AspNetCore.Mvc.ControllerBase.
Introduction to Swagger
1. Swasbuckle.AspNetCore
2. Swagger
3. Open API
API Versions
API Versioning is the practice of transparently managing changes to your API, where the
client requests a specific version of API; and the server executes the same version of the
API code.
Content Negotiation
Content negotiation is the process of selecting the appropriate format or language of the
content to be exchanged between the client (browser) and Web API.
Overview of Minimal API
- It is a Microsoft's API that is used to create HTTP services (or HTTP APIs) with minimal
dependencies on packages.
- Alternative to Web API Controllers. Mainly used to create HTTP services or Microservices.
● Limited support for custom model binding and custom model validation (needs to
improve).
● No support for views.
● No support for filters & filter pipeline; but supports "Endpoint Filters" alternatively.
1. MapGet()
2. MapPost()
3. MapPut()
4. MapDelete()
Route parameters can be created as you were creating them in UseEndpoints() or in MVC
controllers.
Route constraints can be used as you were using them in UseEndpoints() or in MVC
controllers.
Eg: int, bool, datetime, decimal, double, float, guid, long, Minlength, maxlength, length, min,
max, range, alpha, regex, required
A map group (or route group) is a set of endpoints with a common prefix.
A map group is a collection of endpoints created with Map* methods such as MapGet(),
MapPost() etc.
MapGet()
mapGroup.MapGet(…);
mapGroup.MapPost(..);
IResult
1. Results.Ok( )
2. Results.Json( )
3. Results.Text( )
4. Results.File( )
5. Results.BadRequest( )
6. Results.NotFound( )
7. Results.Unauthorized( )
8. Results.ValidationProblem( )
IResult Implementations
1. Results.Ok
2. Results.Json
3. Results.Text
4. Results.File
5. Results.BadRequest
6. Results.NotFound
7. Results.Unauthorized
8. Results.ValidationProblem
Endpoint Filter
EndPoint Filters execute much like 'action filters' i.e., 'before' and 'after' the execution of
minimal API endpoint.
app.MapGet("/route", () => {
//your endpoint code here
})
.AddEndpointFilter(async (context, next) => {
//before logic
var result = await next(context); //calls subsequent filter or endpoint
//after logic
return result;
});
IEndpointFilter
//after logic
return result;
}
}