81 ASPNET Core API Controller-Based API
81 ASPNET Core API Controller-Based API
CHAPTER IV
ASP.NET CORE API - Controller based
PHAM THAO
OUTLINE
https://learn.microsoft.com/vi-vn/aspnet/core/tutorials/min-web-api?view=aspnetcore-7.0&tabs=visual-studio
CREATE A NEW PROJECT
ASP.NET Core Web API:
This template is designed for building RESTful APIs
without server-rendered views.
It includes controllers that respond to HTTP requests
and return data in JSON or other formats.
Perfect for creating backend services or APIs that serve
data to various client applications, such as web, mobile, or
desktop apps.
It focuses on data exchange and does not include views
or HTML rendering capabilities.
ASP.NET CORE WEB API
Minimal API
Minimal APIs are architected to
create HTTP APIs with minimal
dependencies. They are ideal for
microservices and apps that want to
include only the minimum files,
features, and dependencies in
ASP.NET Core.
Controller-based APIs
ASP.NET CORE WEB API – MINIMAL API
Minimal API
Minimal APIs are
architected to create
HTTP APIs with minimal
dependencies. They are
ideal for microservices
and apps that want to
include only the
minimum files, features,
and dependencies in
ASP.NET Core.
Controller-based APIs
ASP.NET CORE WEB API – MINIMAL API
ASP.NET CORE WEB API – MINIMAL API
Add NuGet packages
Microsoft.EntityFrameworkCore.InMemory
Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore
THE MODEL AND DATABASE CONTEXT CLASSES
The sample app code repeats the The preceding code has the following changes:
todoitems URL prefix each time it
sets up an endpoint. Adds var todoItems =
APIs often have groups of
app.MapGroup("/todoitems"); to set up the group
endpoints with a common URL using the URL prefix /todoitems.
prefix, and
the MapGroup method is Changes all the app.Map<HttpVerb> methods to
available to help organize such todoItems.Map<HttpVerb>.
groups.
It reduces repetitive code and
Removes the URL prefix /todoitems from the
allows for customizing entire Map<HttpVerb> method calls.
groups of endpoints with a
single call to methods Test the endpoints to verify that they work the
like RequireAuthorization and same.
WithMetadata.
USE THE TYPEDRESULTS API
{ "Authentication": {
"DefaultScheme": "LocalAuthIssuer",
"Schemes": { "Bearer": {
"ValidAudiences": [
"https://localhost:7259",
"http://localhost:5259" ],
"ValidIssuer": "dotnet-user-jwts" },
"LocalAuthIssuer": {
"ValidAudiences": [
"https://localhost:7259",
"http://localhost:5259" ],
"ValidIssuer": "local-auth" } } } }
ADD AUTHENTICATION
ADD AUTHENTICATION
ADD AUTHENTICATION – WRONG REQUIRE