The current version of Microsoft's popular cross-platform programming framework is.NET 7. Minimal APIs, a new technique to construct lightweight, quick, and simple APIs with just a few lines of code, is one of the new features introduced in.NET 7. In this post, we'll look at how to use Minimal APIs in.NET 7, as well as how to add MapGroup() to a static class.

What exactly are Minimal APIs?

Minimal APIs are a new type of API introduced in.NET 7, allowing developers to construct APIs with minimal overhead and optimal efficiency. They are lightweight and simple to use, with an emphasis on simplicity and ease of development.How to create a Minimal API in .NET 7?

Creating a Minimal API in .NET 7 is simple. Here’s an example of how to create a Minimal API that returns “Hello World!” when you make a GET request to the root URL:
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/", () => "Hello World!");
app.Run();

In this example, we’ve used the WebApplication class to create a new Minimal API. We've then used the MapGet() method to map a GET request to the root URL ("/") and return "Hello World!".
Adding MapGroup() to a static class

MapGroup() allows you to group similar endpoints and apply shared middleware or configuration to them. To add MapGroup() to a static class, follow these steps:

1. Create a static class for your endpoints
public static class MyEndpoints
{
    public static void MapGroup(this IEndpointRouteBuilder endpoints)
    {
        endpoints.MapGet("/", Get).WithName("Get").WithOpenApi();

        endpoints.MapGet("/api/customers", GetCustomers).WithName("Customers").WithOpenApi();

        // Add more endpoints here
    }

    private static IResult Get()
    {
        return Results.Ok("Hello World!");
    }

    private static IResult GetCustomers()
    {
        return Results.Ok("List of customers");
    }
}


2. Add a MapGroup() call your endpoint configuration in the Startup class, and configure OpenAPI and Swagger services.
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.MapGroup();
app.Run();


In this example, we’ve added a call to MapGroup() and passed in an empty string as the group prefix and the MapGroup() method from our MyEndpoints static class.

Now, all the endpoints defined in MyEndpoints the class will be grouped under the specified prefix, which in this case, is an empty string. You can add a different prefix to group endpoints together and use the same prefix in your middleware or configuration for all endpoints in the group.

With MapGroup(), you can create modular and organized APIs with shared middleware and configuration for groups of endpoints.

Minimal APIs in.NET 7 make it simple to write lightweight, quick APIs with only a few lines of code. You can group comparable endpoints and apply shared middleware or configuration to them by adding MapGroup() to a static class. You can use these characteristics to construct simple and organized APIs that are straightforward to design and maintain.