What are Middleware and the Request Pipeline in ASP.NET Core?
π‘ Concept Name
Middleware & Request Pipeline in ASP.NET Core
π Quick Intro
Middleware are components that handle HTTP requests and responses. In ASP.NET Core, they form a pipeline where each middleware can modify the request or response. The order of middleware affects how the request flows through the app.
π§ Analogy / Short Story
Imagine airport security. Your request (passenger) goes through multiple checks β ID, baggage scan, boarding pass β before reaching the gate (final destination). Each checkpoint represents middleware. Some may stop you (authentication), some just log you (logging), some may let you through. The order matters. If you flip ID check and boarding pass check, the process breaks.
π§ Technical Explanation
In ASP.NET Core, middleware are arranged in a sequence during application startup. Each middleware can:
- Inspect the incoming request
- Pass it to the next middleware
- Modify the response on the way back
Common middleware includes Routing, Static Files, Authentication, Authorization, and Exception Handling. They're configured in `Program.cs` or `Startup.cs` using `app.Use...()` methods.
π― Purpose & Use Case
- β Handle HTTP requests and responses in a structured way
- β Log requests, handle exceptions, or validate headers
- β Use built-in or custom middleware for flexibility
- β Control request flow using routing and endpoints
- β Build reusable components like authentication, CORS, compression, etc.
π» Real Code Example
Program.cs β configuring middleware
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.UseHttpsRedirection(); // Middleware 1
app.UseRouting(); // Middleware 2
app.UseAuthentication(); // Middleware 3
app.UseAuthorization(); // Middleware 4
app.MapControllers(); // Endpoint middleware
app.Run();
Custom middleware example:
public class RequestLoggerMiddleware
{
private readonly RequestDelegate _next;
public RequestLoggerMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task InvokeAsync(HttpContext context)
{
Console.WriteLine($""Request Path: {context.Request.Path}"");
await _next(context); // Call the next middleware
}
}
// Register in Program.cs
app.UseMiddleware<RequestLoggerMiddleware>();

β Interview Q&A
Q1: What is middleware?
A: Middleware is software that processes HTTP requests and responses.
Q2: Where is middleware configured?
A: In `Program.cs` or `Startup.cs` using `app.Use...()` methods.
Q3: What does the order of middleware affect?
A: It affects how the request and response are handled.
Q4: Can we write custom middleware?
A: Yes, by creating a class and using `app.UseMiddleware<T>()`.
Q5: Whatβs the difference between middleware and controller?
A: Middleware handles the request pipeline; controllers process final requests mapped to routes.
Q6: What is terminal middleware?
A: Middleware that does not call `await _next()` and ends the pipeline.
Q7: What is endpoint routing?
A: Middleware that maps HTTP requests to route handlers or controllers.
Q8: How to add exception handling middleware?
A: Use `app.UseExceptionHandler()` or `app.UseDeveloperExceptionPage()`.
Q9: Is middleware synchronous or asynchronous?
A: It's fully async for performance and scalability.
Q10: Can middleware access services from DI?
A: Yes, via constructor injection or `HttpContext.RequestServices`.
Q1: What is middleware in ASP.NET Core?
- A. HTML Parser
- B. NuGet Package
- C. A Razor Page
- D. HTTP request/response handler
Q2: Where is middleware registered?
- A. Program.cs or Startup.cs
- B. web.config
- C. launchSettings.json
- D. Services.cs
Q3: What is terminal middleware?
- A. Ends the request pipeline
- B. Calls next middleware
- C. Redirects to IIS
- D. Used only for GET requests
Q4: What method defines custom middleware?
- A. AddMiddleware()
- B. UseMiddleware<T>()
- C. Middleware.Build()
- D. ConfigureMiddleware()
Q5: Can middleware be async?
- A. No
- B. Only for GET requests
- C. Yes, fully supported
- D. Yes, but only in Startup.cs
π PDF Download
Need a handy summary for your notes? Download this topic as a PDF!