A static-file server returns requested files directly from storage instead of generating their contents for each request. In ASP.NET Core, static-file middleware provides this focused responsibility within the middleware pipeline. section 4.2 section 4.0
Use static-file middleware as one small part of a composed request pipeline:
- Call UseStaticFiles() to serve files from the wwwroot folder, such as images. section 4.2.3
- Combine it with routing, endpoint, and exception-handler middleware to form a realistic minimal API application. section 4.2.3
- Place middleware deliberately because the order of Use*() calls defines request handling behavior. section 4.2.3
Serving files in a minimal API
A basic application adds app.UseStaticFiles() before routing and maps a separate root endpoint. Requests for matching files in wwwroot can be served as static files, while the root path uses the mapped Hello World endpoint. section 4.2.3
Figure 4.10 A simple minimal API application. The application uses only four pieces of middleware: routing middleware to choose the endpoint to run, endpoint middleware to generate the response from a Razor Page, static-file middleware to serve image files, and exception-handler middleware to capture any errors.
The pipeline figure shows static-file middleware serving image files alongside routing, endpoint generation, and exception handling. section 4.2.3
