liveBook · Manning

Wwwroot folder

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

The wwwroot folder is the conventional ASP.NET Core application directory whose files are publicly served as static content.

The wwwroot folder is the conventional ASP.NET Core home for public static files. Store CSS, JavaScript, images, and static HTML there; by default, ASP.NET Core serves files only from this folder, helping keep other application folders private. section 4.2.2 section 13.1.1

Static-file requests follow a simple pattern:

  • A request such as /moon.jpg is checked against the wwwroot folder. section 4.2.2
  • When the file exists, StaticFileMiddleware returns it to the browser. section 4.2.2
  • When the file is missing, the application returns an HTTP 404 response. section 4.2.2
  • You may organize files inside wwwroot however you want; template subfolders are optional. section 13.1.1

Enable static files

A minimal application needs the static-file middleware in its pipeline: WebApplicationBuilder builder = WebApplication.CreateBuilder(args); WebApplication app = builder.Build(); app.UseStaticFiles(); app.Run(); With moon.jpg stored in wwwroot, requesting /moon.jpg returns the image. section 4.2.2

Figure 4.9 StaticFileMiddleware handles a request for a file. The middleware checks the wwwroot folder to see if whether requested moon.jpg file exists. The file exists, so the middleware retrieves it and returns it as the response to the web server and, ultimately, to the browser.
CH04_F09_Lock3

Trace how StaticFileMiddleware checks wwwroot, retrieves an existing file, and returns it through the web server to the browser. section 4.2.2

Deployment preserves this arrangement: dotnet publish copies the wwwroot folder and its contents into the publish output, so static assets travel with the application. section 27.1.1

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

How deep it goes
Explains folder behavior, middleware configuration, request outcomes, organization, and publishing, but not custom file-provider configuration.
Examples
Uses moon.jpg, missing.jpg, UseStaticFiles, and dotnet publish to demonstrate serving, failure, configuration, and deployment.
What it assumes
Assumes a basic ASP.NET Core application and teaches static-file serving without requiring additional services.
What you can do after
Build it from scratch by placing assets in wwwroot, enabling UseStaticFiles, testing URLs, and publishing the application.
How it connects
Connects static files with StaticFileMiddleware, the middleware pipeline, Razor Pages, and application deployment.

Also explained in

Compare how 2 books explain wwwroot folder →

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.