.Net 6 - Minimal API
Minimal API introduced with .net 6 and this is very good in sense:
- Best suited for micro services to put your related endpoints in project.
- This is light weight compare to .net core standard(MVC) API pattern and it contains less no. of files like do not need to create controllers.
- Less no. of lines of codes to write to create endpoint.
- Single file API, to put all endpoints related to that project with program.cs file.
- You should be able to get all the benefits and features similar to .net core standard(MVC) pattern API like:
- Creating different methods GET, POST, PUT etc..
- Defining routes for API.
- Sending query string to API.
- Sending headers to API.
- Dependency injection like .net core (injecting objects).
- Async, Await call.
- Authentication/Authorization.
- Support other packages which you had used earlier like Fluent validation to create validation for your API.
- Etc.. We will see all these in below demo.
Demo:
While you create ".Net core web api" in visual studio, there is checkbox option for minimal API, so you need to unselect that, otherwise it will create normal MVC pattern for your API. Refer below screenshot:
Once you created, by default it give you weather forecast related APIs, which you get even in case of .net core standard API. But all those action methods are inside program.cs file and if you see the solution explorer, it just having few files and not controllers at all and it also by default provide you swager configuration. So when you run your application, it will open up swagger, where you can use the default APIs available. Refer below screenshot:
Note: Mainly here we use "MapGet", "MapPost", "MapPut" etc. methods to setup respective APIs as on need.
Demo 1: GET, POST, Async, Query String Test
- Created below "UserManager.cs" file where I have created "User" as record struct type with Id and Name property.
- With UserManager class, I have created:
- "GetUsers() to get list of dummy users".
- "GetUser() with Id input to filter user and get specific user".
- "AddUser() with User record type post input to add user".
- "GetUsersAsyncTest() to test async call from minimal API".
- With "Program.cs" I have used above "UserManager" and created endpoints.
- First API I created Get type to check heartbeat of my application.
- Second API for getting list of users.
- Third API to get specific user as per Id which I am sending as query string and if you see, I have used two different way to send query string.
- Fourth API is post type to add user, where I sending "User" record struct as request body and in result will get new user id with http status code to OK(200).
- Fifth API is to see, how we can use async and await with minimal API.
Demo 2: DI (dependency injection): with demo 1, if you see, I have created "UserManager" class object myself and then used that object to call respective methods inside respective API. Now instead of creating self object, I want to use/setup DI with minimal API, how we do in .net core startup.cs file to inject class and then as per scope defined, it create object available in constructor of that class. So,
- First you have to configure the respective class with scope. With below example, I am creating singleton object for my UserManager class.
- Then with API method, you have to use "[FromServices] attribute for your class, so that while using that class object it will pull the object allocation from the services injection.
Demo 3: With this demo, I want to receive/capture header input sent from the consumer and to do that you have to use "[FromHeader] attribute".
- With below API, I am creating variable "AuthenticationToken" which will be send into header while calling this API.
Demo 4: Add JWT Authentication/Authorization with minimal API.
- With below example, marked in "Yellow" color, where I have enabled "JWT" token for my endpoints.
- To enable authentication with each individual API, we can use either "[Authorize] attribute" or "RequireAuthorization() method", refer below marked in yellow both the examples.
- To enable AllowAnonymous, you have to use either "[AllowAnonymous] attribute" or "AllowAnonymous() method", refer below marked in yellow both the examples.
- With below example marked in "Turquoise" color, where I have enabled/configured header input for JWT token on swagger.
- Also to use JWT token, you have to install JwtBearer package, refer below screenshot:
using Microsoft.AspNetCore.Authentication.JwtBearer; //JWTToken for auth
using Microsoft.AspNetCore.Authorization;
using Microsoft.OpenApi.Models; //AuthTest
var builder = WebApplication.CreateBuilder(args);
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
//builder.Services.AddSwaggerGen();
//Add swagger to have Bearer token option available for header input
builder.Services.AddSwaggerGen(config => {
config.AddSecurityDefinition("Bearer",
new OpenApiSecurityScheme {
Description = "JWT header token using 'bearer' scheme",
Name = "BearerToken", //Bearer
In = ParameterLocation.Header,
Type = SecuritySchemeType.ApiKey
});
config.AddSecurityRequirement(new OpenApiSecurityRequirement {
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference {
Id = "Bearer", Type = ReferenceType.SecurityScheme
}
},
new List<string>()
}
});
});
//AuthTest
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer();
//This is require to enable authorization for all the endpoints, instead of using [Authorize] attribute to each individual endpoint
builder.Services.AddAuthorization(options => {
options.DefaultPolicy = new AuthorizationPolicyBuilder().
AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme).
RequireAuthenticatedUser().Build();
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment()) {
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseAuthorization();
app.UseAuthentication();
app.MapGet("/AuthTest-AllowAnonymous", [AllowAnonymous] () => "This endpoint is set for AllowAnonymous."); //Url: https://localhost:7176/AuthTest-AllowAnonymous
app.MapGet("/AuthTest-AllowAnonymous-Option2", () => "This endpoint is set for AllowAnonymous.").AllowAnonymous(); //Url: https://localhost:7176/AuthTest-AllowAnonymous
app.MapGet("/AuthTest-Authorize", [Authorize] () => "This endpoint is set for AllowAnonymous."); //Url: https://localhost:7176/AuthTest-Authorize
app.MapGet("/AuthTest-Authorize-Option2", () => "This endpoint is set for AllowAnonymous.").RequireAuthorization(); //Url: https://localhost:7176/AuthTest-Authorize-Option2
app.Run();
Result of above code:
Categories/Tags: minimal api~.net 6