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

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay