ASP.NET Core Middleware & 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.InsertMiddleware()
Correct answer: app.UseMiddleware()
app.UseMiddleware<T>() registers a custom middleware class in the request pipeline.
Question 2: Which interface must a custom middleware class implement to be recognized by ASP.NET Core's IMiddlewareFactory?
- IMiddleware (Correct answer)
- IRequestHandler
- IPipelineComponent
- IHttpMiddleware
Correct answer: IMiddleware
IMiddleware defines the InvokeAsync method that the factory-activated middleware pattern requires.
Question 3: What is the purpose of the 'next' delegate in ASP.NET Core middleware?
- To call the next middleware in the pipeline (Correct answer)
- To return a response immediately
- To log the request
- To authenticate the user
Correct answer: To call the next middleware in the pipeline
Calling next(context) passes control to the subsequent middleware component in the pipeline.
Question 4: Which middleware method creates a branch in the pipeline that does NOT rejoin the main pipeline?
- app.Run() (Correct answer)
- app.Use()
- app.Map()
- app.Branch()
Correct answer: app.Run()
app.Run() adds terminal middleware that never calls next, ending the pipeline branch.
Question 5: What does app.Map() do in an ASP.NET Core middleware pipeline?
- Branches the pipeline based on a URL path match (Correct answer)
- Maps a route to a controller
- Registers middleware globally
- Creates a new application instance
Correct answer: Branches the pipeline based on a URL path match
app.Map() creates a conditional branch in the pipeline that activates when the request path matches.
Question 6: In what order should UseRouting() and UseAuthorization() be called in the ASP.NET Core pipeline?
- UseRouting() before UseAuthorization() (Correct answer)
- UseAuthorization() before UseRouting()
- Order does not matter
- They cannot be used together
Correct answer: UseRouting() before UseAuthorization()
UseRouting() must precede UseAuthorization() so that route data is available when authorization policies are evaluated.
What method is used to add middleware to the ASP.NET Core request pipeline?