C# Interview Question #162

What is middleware in ASP.NET Core?

ASP.NET Core, DI & Middleware Senior Advanced

Quick Interview Answer

Middleware is software assembled into the HTTP request pipeline. Each middleware component can inspect or modify the request, perform work before and after the next component, and optionally terminate the pipeline.

Detailed Explanation

Middleware is software assembled into the HTTP request pipeline. Each middleware component can inspect or modify the request, perform work before and after the next component, and optionally terminate the pipeline.

Common middleware handles exception processing, HTTPS redirection, static files, routing, authentication, authorization, CORS, response compression, and logging.

Middleware order matters because requests pass through the pipeline in registration order and responses travel back through it in reverse order.

Code Example

var app = builder.Build();

app.UseExceptionHandler("/Home/Error");
app.UseHttpsRedirection();
app.UseStaticFiles();

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

app.MapControllers();