liveBook · Manning

UseStaticFiles

As explained in Pro ASP.NET Core 7, Tenth Edition

UseStaticFiles is an ASP.NET Core application-builder method that adds static-file serving to the request pipeline.

UseStaticFiles is an ASP.NET Core application-builder method that adds static-file middleware to the request pipeline. Without arguments, it serves matching files from wwwroot; matching requests receive file contents and response headers such as Content-Type and Content-Length, while other requests continue through the pipeline. section 7.1.5 section 15.4.1

Configure the middleware according to the URL and file-location mapping you need:

  • Call app.UseStaticFiles() near the start of the pipeline so later components do not handle requests intended for static files. section 15.4.1
  • Pass StaticFileOptions to select another file location with FileProvider and assign its URL prefix with RequestPath. section 15.4.1
  • Use the string overload when only a RequestPath prefix is needed, or register multiple middleware instances for separate URL-to-folder mappings. section 15.4.1
  • Customize content-type behavior with ContentTypeProvider or DefaultContentType, and adjust responses through OnPrepareResponse. section 15.4.1

Expose an additional static-files folder

var env = app.Environment; app.UseStaticFiles(new StaticFileOptions { FileProvider = new PhysicalFileProvider($"{env.ContentRootPath}/staticfiles"), RequestPath = "/files" }); With hello.html stored in staticfiles, request /files/hello.html to receive that file. Keep the parameterless call as well when wwwroot should remain available.

How Pro ASP.NET Core 7, Tenth Edition covers it

How deep it goes
Explains default behavior, middleware ordering, response headers, configurable locations, URL prefixes, MIME handling, and multiple mappings.
Examples
Uses wwwroot files, a static.html request, and a PhysicalFileProvider mapping from /files to staticfiles.
What it assumes
Assumes basic ASP.NET Core application-builder and request-pipeline usage while situating static files alongside MVC and endpoint routing.
What you can do after
Operate/configure static-file serving with default wwwroot behavior or customized file providers, URL prefixes, and response options.
How it connects
Connects static-file middleware with the request pipeline, endpoint routing, MVC, and application-builder configuration.

Also explained in

Compare how 2 books explain UseStaticFiles →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro ASP.NET Core 7, Tenth Edition. The section links point to the parts of the book it draws on.