ASP.NET Hosting

ASP.NET Tutorial: In .NET Core, There Are Three Ways to Define Middleware

The powerful idea of middleware in.NET Core enables programmers to intercept, process, and reply to HTTP requests in a pipeline that may be customized. Middleware is a crucial component of request processing since each component can function both before and after the subsequent component runs. With examples and use cases, this article examines three popular approaches to implementing middleware in.NET Core:

  1. Inline Middleware
  2. Custom Middleware
  3. Extension Method Middleware

Inline Middleware

Inline middleware is the simplest and most direct way to define middleware. It’s ideal for quick logic, prototypes, or lightweight request processing.

Example. Logging Request and Response

public void Configure(IApplicationBuilder app)
{
    app.Use(async (context, next) =>
    {
        Console.WriteLine("Request: " + context.Request.Path);
        await next.Invoke();
        Console.WriteLine("Response: " + context.Response.StatusCode);
    });

    app.Run(async context =>
    {
        await context.Response.WriteAsync("Hello from inline middleware!");
    });
}

Use Case

• Quick logging
• Diagnostics
• Simple request filtering

Custom Middleware

A custom middleware is a more modular and reusable approach. You define a separate class that encapsulates the logic and can be used across multiple projects.

Example. Logging Middleware

public class LoggingMiddleware
{
    private readonly RequestDelegate _next;

    public LoggingMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    public async Task InvokeAsync(HttpContext context)
    {
        Console.WriteLine("Logging request: " + context.Request.Path);
        await _next(context);
        Console.WriteLine("Logging response: " + context.Response.StatusCode);
    }
}

Register in Startup.cs

public void Configure(IApplicationBuilder app)
{
    app.UseMiddleware<LoggingMiddleware>();
}

Use Case

Complex logic

  • Reusable components
  • Better separation of concerns

Extension Method Middleware

The extension method middleware approach encapsulates middleware registration logic into a clean, reusable extension method. This keeps your Startup or Program class neat and readable.

Example. Logging Middleware Extension

public static class LoggingMiddlewareExtensions
{
    public static IApplicationBuilder UseLogging(this IApplicationBuilder builder)
    {
        return builder.UseMiddleware<LoggingMiddleware>();
    }
}

Use in Startup.cs

public void Configure(IApplicationBuilder app)
{
    app.UseLogging();
}

Use Case

  • Clean and fluent middleware registration
  • Consistent usage across projects
  • Simplifies pipeline configuration

Summary Table

Method Flexibility Reusability Best For
Inline Middleware Low None Quick tasks, debugging, or prototyping
Custom Middleware High High Modular, testable components
Extension Method Middleware Medium High Clean registration, reusable APIs

Key Takeaway

Middleware in .NET Core is composable, meaning you can chain multiple components together to build powerful pipelines. Start with inline middleware for quick experiments, and evolve to custom and extension-based middleware for cleaner, maintainable architectures.

ASP.NET Core 10.0 Hosting Recommendation

HostForLIFE.eu
HostForLIFE.eu is a popular recommendation that offers various hosting choices. Starting from shared hosting to dedicated servers, you will find options fit for beginners and popular websites. It offers various hosting choices if you want to scale up. Also, you get flexible billing plans where you can choose to purchase a subscription even for one or six months.

We learned the new technique and evolved together.

Happy coding!