---
title: "How is middleware configured?"  
description: "How is middleware configured?"  
author: "Anubhav Sharma"  
published: 2025-05-19  
updated: 2025-05-19  
canonical: https://www.mindstick.com/interview/34142/how-is-middleware-configured  
category: "c#"  
tags: ["c#", ".net core"]  
reading_time: 4 minutes  

---

# How is middleware configured?

Middleware in ASP.NET Core is configured by **adding it to the HTTP request pipeline** inside the `Startup.Configure` method (or in `Program.cs` for .NET 6 and later). This pipeline is built using the `IApplicationBuilder` interface with a series of method calls like `UseMiddleware()`, `UseRouting()`, `UseAuthentication()`, and so on.

## How to Configure Middleware — Step by Step

### 1. Inside `Startup.Configure` (Pre .NET 6 style)

```cs
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage(); // Exception handling middleware
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
        app.UseHsts();
    }

    app.UseHttpsRedirection();  // Redirect HTTP to HTTPS
    app.UseStaticFiles();       // Serve static files

    app.UseRouting();           // Adds routing middleware

    app.UseAuthentication();   // Add authentication middleware
    app.UseAuthorization();    // Add authorization middleware

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();  // Map controller routes
    });
}
```

1. Middleware components are executed **in the order** they are added.
2. You use `.UseXyz()` methods to add middleware that **passes control** to the next component.
3. `.Run()` can be used to **terminate the pipeline** early (not common except for very specific cases).

### 2. In `Program.cs` (Minimal Hosting Model — .NET 6 and later)

```cs
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddAuthentication();
builder.Services.AddAuthorization();
builder.Services.AddControllers();

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
}
else
{
    app.UseExceptionHandler("/Error");
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

app.MapControllers();

app.Run();
```

### 3. Adding Custom Middleware

You can add your own middleware with:

```cs
app.Use(async (context, next) =>
{
    // Code before next middleware runs
    Console.WriteLine("Request Incoming");

    await next();  // Call the next middleware

    // Code after next middleware runs
    Console.WriteLine("Response Outgoing");
});
```

Or create a class middleware and add it with:

```cs
app.UseMiddleware<MyCustomMiddleware>();
```

### Summary

1. **Middleware is configured by calling extension methods on** `IApplicationBuilder` inside the `Configure` method or the minimal hosting `Program.cs`.
2. **Order matters:** The order you add middleware affects how requests and responses flow.
3. Use `.Use...()` for middleware that calls the next one, `.Run()` for terminal middleware.

## Answers

### Answer by Anubhav Sharma

Middleware in ASP.NET Core is configured by **adding it to the HTTP request pipeline** inside the `Startup.Configure` method (or in `Program.cs` for .NET 6 and later). This pipeline is built using the `IApplicationBuilder` interface with a series of method calls like `UseMiddleware()`, `UseRouting()`, `UseAuthentication()`, and so on.

## How to Configure Middleware — Step by Step

### 1. Inside `Startup.Configure` (Pre .NET 6 style)

```cs
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage(); // Exception handling middleware
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
        app.UseHsts();
    }

    app.UseHttpsRedirection();  // Redirect HTTP to HTTPS
    app.UseStaticFiles();       // Serve static files

    app.UseRouting();           // Adds routing middleware

    app.UseAuthentication();   // Add authentication middleware
    app.UseAuthorization();    // Add authorization middleware

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();  // Map controller routes
    });
}
```

1. Middleware components are executed **in the order** they are added.
2. You use `.UseXyz()` methods to add middleware that **passes control** to the next component.
3. `.Run()` can be used to **terminate the pipeline** early (not common except for very specific cases).

### 2. In `Program.cs` (Minimal Hosting Model — .NET 6 and later)

```cs
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddAuthentication();
builder.Services.AddAuthorization();
builder.Services.AddControllers();

var app = builder.Build();

if (app.Environment.IsDevelopment())
{
    app.UseDeveloperExceptionPage();
}
else
{
    app.UseExceptionHandler("/Error");
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

app.MapControllers();

app.Run();
```

### 3. Adding Custom Middleware

You can add your own middleware with:

```cs
app.Use(async (context, next) =>
{
    // Code before next middleware runs
    Console.WriteLine("Request Incoming");

    await next();  // Call the next middleware

    // Code after next middleware runs
    Console.WriteLine("Response Outgoing");
});
```

Or create a class middleware and add it with:

```cs
app.UseMiddleware<MyCustomMiddleware>();
```

### Summary

1. **Middleware is configured by calling extension methods on** `IApplicationBuilder` inside the `Configure` method or the minimal hosting `Program.cs`.
2. **Order matters:** The order you add middleware affects how requests and responses flow.
3. Use `.Use...()` for middleware that calls the next one, `.Run()` for terminal middleware.


---

Original Source: https://www.mindstick.com/interview/34142/how-is-middleware-configured

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
