Explain Middleware in ASP.NET Core

πŸ“˜ Introduction

Middleware in ASP.NET Core are software components that are assembled into an application pipeline to handle requests and responses. Each middleware component:

  • Can perform operations before and after the next component in the pipeline.
  • Can terminate the pipeline (short-circuiting).
  • Handles cross-cutting concerns like logging, authentication, etc.

🧠 Real-Life Analogy

Imagine airport security. Every passenger goes through multiple layersβ€”ID check, baggage scan, pat-down, etc. Each is a middleware component in the processing pipeline. Similarly, in ASP.NET Core, requests flow through multiple middleware layers before reaching the final destination (the controller or endpoint).

πŸ’‘ Key Concepts

  • Use: Adds middleware that can call the next delegate.
  • Run: Final delegate in the pipeline. No next.
  • UseMiddleware: Adds custom middleware using a class.

πŸ§ͺ Example Code

// In Program.cs or Startup.cs
app.Use(async (context, next) => {
    Console.WriteLine("Middleware 1: Before");
    await next();
    Console.WriteLine("Middleware 1: After");
});

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

πŸ’¬ Interview Q&A

  • Q: What is middleware in ASP.NET Core?
  • A: Middleware is software that handles requests/responses in a pipeline.
  • Q: What does app.Run() do?
  • A: It terminates the pipeline and handles the request directly.
  • Q: What is the purpose of next() in middleware?
  • A: To pass control to the next middleware in the pipeline.
  • Q: Can we have multiple app.Run() calls?
  • A: Only one will execute because it terminates the pipeline.
  • Q: How do you register custom middleware?
  • A: Using app.UseMiddleware<MyMiddleware>().
  • Q: What is the order of execution?
  • A: Middleware is executed in the order it is added.
  • Q: What is the difference between Use and Run?
  • A: Use allows next middleware to run; Run terminates.
  • Q: Can middleware be asynchronous?
  • A: Yes, middleware typically uses async/await.
  • Q: Is middleware reusable?
  • A: Yes, it's modular and reusable.
  • Q: How to handle exceptions globally in middleware?
  • A: Use built-in UseExceptionHandler() middleware or write a custom one.

πŸ“ MCQs

πŸ“ MCQs

Q1. Which delegate terminates the middleware pipeline?

  • Use
  • Run
  • Next
  • Handle

Q2. What keyword is used to call the next middleware?

  • continue
  • await next()
  • callNext()
  • proceed()

Q3. Can middleware be ordered?

  • No
  • Yes
  • Depends
  • Not applicable

Q4. What is UseMiddleware used for?

  • Handle errors
  • Set headers
  • Register custom middleware
  • Terminate pipeline

Q5. Which middleware logs requests?

  • Static files
  • Authentication
  • Logging middleware
  • MVC

Q6. What is the correct order to add Use, Run?

  • Run first, Use later
  • Use then Run
  • Doesn’t matter
  • Only Use

Q7. Can middleware write response?

  • No
  • Yes
  • Sometimes
  • Only in Run

Q8. What is the type of middleware class?

  • IService
  • IMiddleware
  • IRequest
  • IHandler

Q9. What does app.Use() return?

  • Task
  • void
  • IApplicationBuilder
  • Action

Q10. Is middleware framework-dependent?

  • Yes
  • No
  • Only in IIS
  • Only in Kestrel

πŸ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

πŸ’¬ Feedback
πŸš€ Start Learning
Share:

Tags: