Codecademy Logo

Introduction to ASP.NET Middleware

Related learning

  • Learn how to build fast, secure, and maintainable web apps with ASP.NET and the Razor Pages architecture.
    • With Certificate
    • Intermediate.
      17 hours
  • Jumpstart your career with this skill path, first by learning the C# language, then building web apps with ASP.NET Core and the Razor Pages.
    • Includes 7 Courses
    • With Certificate
    • Intermediate.
      41 hours

Understanding ASP.NET middleware

Web applications use a series of components to route each HTTP request to its destination and then return an appropriate response to the user. This series of components is organized in a pipeline which is collectively known as middleware.

Accessing Built-in ASP.NET Components

The IApplicationBuilder interface defines the built-in middleware methods. These methods are defined by using the format UseX() with X describing the action performed by the method.

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)

Adding custom middleware components

The IApplicationBuilder interface provides a generic Use() method which can be used to process custom middleware components and call the next component in the pipeline. Multiple middleware components can be chained together with the Use() method, which accepts two parameters, the HTTP context and the next middleware component to be called.

app.Use(async (context, next) => {
await context.Response.WriteAsync("Custom middleware!");
await next();
});

Adding terminal ASP.NET middleware

IApplicationBuilder.Run() is a terminal middleware component which begins returning the HTTP response. Run() can be added to the end of the request pipeline since any delegates after this component will not be executed.

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

Understanding ASP.NET Nested Structure

Proper ordering of middleware components is important. The middleware request pipeline has a nested structure where each component can perform operations before and after the next component.

Middleware 1, then Middleware 2, then Middleware 3, then Middleware 2, then Middleware 1

ASP.NET Middleware

In ASP.NET (Core 8), adding middleware is streamlined using the minimal hosting model. Simply inject middleware components in Program.cs with the convenient builder pattern methods. This flexibility allows for tailored request handling pipelines.

using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
// Add custom middleware
app.UseMiddleware<MyCustomMiddleware>();
app.Run();

ASP.NET Core Middleware Pipeline

ASP.NET Core 8 handles requests through a middleware pipeline. Each middleware component processes requests in the order registered in Program.cs. For example, logging should be placed early to capture all events.

// Program.cs
var builder = WebApplication.CreateBuilder(args);
// Register middleware in the desired order
builder.Services.AddLogging();
var app = builder.Build();
// Logging middleware should go first
app.UseMiddleware<LoggingMiddleware>();
app.UseMiddleware<AuthenticationMiddleware>();
app.UseMiddleware<AuthorizationMiddleware>();
app.UseMiddleware<ResponseCompressionMiddleware>();
app.Run();

Learn more on Codecademy

  • Learn how to build fast, secure, and maintainable web apps with ASP.NET and the Razor Pages architecture.
    • With Certificate
    • Intermediate.
      17 hours
  • Jumpstart your career with this skill path, first by learning the C# language, then building web apps with ASP.NET Core and the Razor Pages.
    • Includes 7 Courses
    • With Certificate
    • Intermediate.
      41 hours