liveBook · Manning

Static-file server

As explained in ASP.NET Core in Action, 3e

Static-file middleware is middleware that returns files directly from storage without generating their contents for each request.

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.
CH04_F10_Lock3

The pipeline figure shows static-file middleware serving image files alongside routing, endpoint generation, and exception handling. section 4.2.3

How ASP.NET Core in Action, 3e covers it

How deep it goes
Explains its pipeline role, wwwroot behavior, composition, and ordering, but does not build custom middleware.
Examples
Uses a wwwroot image-serving scenario and a minimal API with UseStaticFiles(), routing, endpoints, and exception handling.
What it assumes
Assumes basic familiarity with ASP.NET Core Program.cs and WebApplication while introducing middleware composition and ordering.
What you can do after
Operate/configure a minimal API pipeline that serves static files alongside mapped endpoints.
How it connects
Connects static-file middleware with middleware pipelines, routing, endpoints, minimal APIs, and exception handling.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.