The Tech Oracle

Exploring Minimal APIs vs. Controllers in ASP.NET Core

When developing APIs in ASP.NET Core, you can choose between two primary approaches: Controllers and Minimal APIs. Each approach has unique characteristics, benefits, and use cases. In this post, we'll dive into the differences between these two approaches, their performance, and scenarios where each shines.

Controllers

Controllers in ASP.NET Core are classes that derive from ControllerBase. They follow a conventional MVC pattern where each controller is responsible for handling HTTP requests, and actions within these controllers map to the application's endpoints.

Here's an example of a basic controller-based API:

namespace APIWithControllers
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            builder.Services.AddControllers();
            var app = builder.Build();

            app.UseHttpsRedirection();
            app.MapControllers();
            app.Run();
        }
    }

    using Microsoft.AspNetCore.Mvc;

    namespace APIWithControllers.Controllers
    {
        [ApiController]
        [Route("[controller]")]
        public class WeatherForecastController : ControllerBase
        {
            private static readonly string[] Summaries = new[]
            {
                "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
            };

            private readonly ILogger<WeatherForecastController> _logger;

            public WeatherForecastController(ILogger<WeatherForecastController> logger)
            {
                _logger = logger;
            }

            [HttpGet(Name = "GetWeatherForecast")]
            public IEnumerable<WeatherForecast> Get()
            {
                return Enumerable.Range(1, 5).Select(index => new WeatherForecast
                {
                    Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
                    TemperatureC = Random.Shared.Next(-20, 55),
                    Summary = Summaries[Random.Shared.Next(Summaries.Length)]
                })
                .ToArray();
            }
        }
    }
}

Minimal APIs

Minimal APIs, introduced in .NET 6, offer a more straightforward way to build APIs using lightweight Lambda expressions. They allow you to define endpoints with logical handlers in-line, within the Main method.

Here's an example using Minimal APIs:

namespace MinimalAPI
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);
            var app = builder.Build();

            app.UseHttpsRedirection();

            var summaries = new[]
            {
                "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
            };

            app.MapGet("/weatherforecast", (HttpContext httpContext) =>
            {
                var forecast = Enumerable.Range(1, 5).Select(index =>
                    new WeatherForecast
                    {
                        Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
                        TemperatureC = Random.Shared.Next(-20, 55),
                        Summary = summaries[Random.Shared.Next(summaries.Length)]
                    })
                    .ToArray();
                return forecast;
            });

            app.Run();
        }
    }
}

Performance Comparison

A detailed performance comparison by Steven Giesel highlighted several benchmark tests between Minimal APIs and Controllers. The study included tests for simple GET requests, requests with injected services, POST requests, middleware handling, and parallel requests.

Results indicated that Minimal APIs generally had a performance edge over Controller-based APIs in certain scenarios. However, the performance differences might be negligible depending on the context and specifics of the project requirements.

When to Use Each Approach

Controllers:\n- Follow a conventional and familiar MVC pattern.\n- Better for applications with complex routing, sophisticated workflows, and larger codebases.\n- Easier to maintain separation of concerns within larger teams.\n\nMinimal APIs:\n- Ideal for microservices and simpler, lightweight applications.\n- Faster setup and usually results in less boilerplate code.\n- Beneficial when rapid development and deployment are prioritized.\n\nUnderstanding the nuances between Minimal APIs and Controllers enables developers to choose the right approach based on the application's needs and team's expertise.\n\n### Conclusion\n\nBoth Minimal APIs and Controllers have their strengths and should be chosen based on the specific needs of your project. Controllers offer a well-structured approach for complex applications, while Minimal APIs provide a more streamlined and faster approach perfect for lightweight applications. \n\nChoose wisely, and always keep your application's requirements at the forefront of your decision-making process.

Comments & Discussion

Comments powered by GitHub Discussions. If comments don't load, please ensure:

  • GitHub Discussions is enabled on the repository
  • You're signed in to GitHub
  • JavaScript is enabled in your browser

You can also comment directly on GitHub Discussions