ASP.NET Core Middleware & Request Pipeline 1 — Questions and Answers
Question 1: What method is used to add middleware to the ASP.NET Core request pipeline?
- app.UseMiddleware() (Correct answer)
- app.AddMiddleware()
- app.RegisterMiddleware()
- app.MapMiddleware()
Correct answer: app.UseMiddleware()
app.UseMiddleware<T>() is the standard method to add custom middleware to the ASP.NET Core pipeline.
Question 2: In what order does ASP.NET Core execute middleware components?
- In reverse order of registration
- In the order they are registered (Correct answer)
- Alphabetically by class name
- Based on priority attribute
Correct answer: In the order they are registered
Middleware executes in the order it is registered in the Configure method or Program.cs.
Question 3: What does calling next() inside middleware do?
- Terminates the request
- Passes control to the next middleware in the pipeline (Correct answer)
- Returns a 200 OK response
- Logs the request
Correct answer: Passes control to the next middleware in the pipeline
Invoking next() passes the HttpContext to the subsequent middleware component in the pipeline.
Question 4: Which built-in middleware should be placed before routing middleware to serve static files?
- UseAuthentication
- UseStaticFiles (Correct answer)
- UseAuthorization
- UseSession
Correct answer: UseStaticFiles
UseStaticFiles() should be called before UseRouting() so static files are served without going through routing.
Question 5: What is a short-circuiting middleware?
- Middleware that throws an exception
- Middleware that calls next() twice
- Middleware that does not call next(), ending the pipeline (Correct answer)
- Middleware that runs asynchronously
Correct answer: Middleware that does not call next(), ending the pipeline
A short-circuiting middleware returns a response without calling next(), preventing further middleware from executing.
Question 6: Which method creates an inline middleware using a delegate?
- app.Use() (Correct answer)
- app.Run()
- app.Map()
- app.Branch()
Correct answer: app.Use()
app.Use() registers an inline middleware delegate that can optionally call next().
What method is used to add middleware to the ASP.NET Core request pipeline?