ASP.NET Core Middleware & Request Pipeline 2 — Questions and Answers
Question 1: What is the purpose of app.Run() in the middleware pipeline?
- Runs the application
- Adds a terminal middleware that never calls next() (Correct answer)
- Starts the web host
- Registers background services
Correct answer: Adds a terminal middleware that never calls next()
app.Run() adds a terminal middleware delegate that handles requests without forwarding to any subsequent middleware.
Question 2: Which middleware enables exception handling and custom error pages in production?
- UseDeveloperExceptionPage
- UseExceptionHandler (Correct answer)
- UseStatusCodePages
- UseErrorBoundary
Correct answer: UseExceptionHandler
UseExceptionHandler() catches unhandled exceptions and redirects to a custom error path in production.
Question 3: What does app.Map() do in the middleware pipeline?
- Maps HTTP verbs to controller actions
- Branches the pipeline based on request path (Correct answer)
- Maps model properties
- Creates route templates
Correct answer: Branches the pipeline based on request path
app.Map() creates a branch in the pipeline that executes when the request path starts with a specified prefix.
Question 4: In which interface must a custom middleware class implement its logic?
- IMiddleware with InvokeAsync method (Correct answer)
- IRequestHandler with Handle method
- IPipeline with Process method
- IHandler with Execute method
Correct answer: IMiddleware with InvokeAsync method
A class-based middleware implements IMiddleware and defines its logic in the InvokeAsync(HttpContext, RequestDelegate) method.
Question 5: What is the correct order for security middleware in ASP.NET Core?
- Authorization → Authentication → CORS
- CORS → Authentication → Authorization (Correct answer)
- Authentication → CORS → Authorization
- CORS → Authorization → Authentication
Correct answer: CORS → Authentication → Authorization
The correct order is UseRouting → UseCors → UseAuthentication → UseAuthorization to ensure security works correctly.
Question 6: Which middleware is responsible for populating HttpContext.User from cookies or tokens?
- UseAuthorization
- UseAuthentication (Correct answer)
- UseSession
- UseCookiePolicy
Correct answer: UseAuthentication
UseAuthentication() processes authentication tokens/cookies and populates HttpContext.User with the authenticated identity.
What is the purpose of app.Run() in the middleware pipeline?