DEV Community

Ravi Ranjan Pandey
Ravi Ranjan Pandey

Posted on

2 2

Create a Web Api using Minimal Api in .NET 6

.NET 6 brings many language improvements in C# 10. Minimal Api is one such feature which allows to create a web api with less boilerplate code. Here is the example which uses Minimal Api for creating an AES Encryption and Decryption api.

using AESEncryptDecrypt;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.MapPost("/encrypt", (string value) =>
{
    string key = app.Configuration["AES:Key"];
    string iv = app.Configuration["AES:AES_IV"];
    return Results.Ok(AESEncDec.AESEncryption(value, key, iv));
});
app.MapPost("/decrypt", (string value) =>
{
    string key = app.Configuration["AES:Key"];
    string iv = app.Configuration["AES:AES_IV"];
    return Results.Ok(AESEncDec.AESDecryption(value, key, iv));
});

app.Run();
Enter fullscreen mode Exit fullscreen mode

Full Code can be found here on Github

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more