ASP.NET Core Middleware & Pipeline 2 — Questions and Answers
Question 1: Which built-in middleware handles serving static files in ASP.NET Core?
- UseStaticFiles() (Correct answer)
- UseFiles()
- ServeStatic()
- UsePublicFiles()
Correct answer: UseStaticFiles()
UseStaticFiles() enables serving files from the wwwroot folder by default.
Question 2: What is the role of the IApplicationBuilder interface in ASP.NET Core?
- It configures the request processing pipeline (Correct answer)
- It registers services in the DI container
- It defines application settings
- It manages database connections
Correct answer: It configures the request processing pipeline
IApplicationBuilder provides methods like Use, Run, and Map to compose the middleware pipeline.
Question 3: Which middleware should be placed early in the pipeline to catch unhandled exceptions?
- UseExceptionHandler() (Correct answer)
- UseRouting()
- UseAuthentication()
- UseHsts()
Correct answer: UseExceptionHandler()
UseExceptionHandler() wraps later middleware so it can catch and handle exceptions from any downstream component.
Question 4: What does app.UseWhen() do in ASP.NET Core?
- Conditionally branches the pipeline and rejoins it (Correct answer)
- Permanently splits the pipeline
- Applies middleware only in development
- Registers conditional services
Correct answer: Conditionally branches the pipeline and rejoins it
app.UseWhen() creates a conditional branch that rejoins the main pipeline after the branch completes.
Question 5: Which middleware is responsible for setting HTTP Strict Transport Security headers?
- UseHsts() (Correct answer)
- UseHttpsRedirection()
- UseCors()
- UseResponseHeaders()
Correct answer: UseHsts()
UseHsts() adds the Strict-Transport-Security header instructing browsers to only use HTTPS.
Question 6: In convention-based middleware, what is the required method signature for the middleware's main method?
- public Task InvokeAsync(HttpContext context, RequestDelegate next) (Correct answer)
- public void Handle(HttpContext context)
- public Task ProcessAsync(HttpContext context)
- public async void Invoke(HttpRequest request)
Correct answer: public Task InvokeAsync(HttpContext context, RequestDelegate next)
Convention-based middleware requires an InvokeAsync(HttpContext, RequestDelegate) method that ASP.NET Core discovers by convention.
Which built-in middleware handles serving static files in ASP.NET Core?