liveBook · Manning

Wwwroot folder

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

The wwwroot folder is the conventional ASP.NET Core project directory from which static web content is served.

The wwwroot folder is the conventional location for static content in an ASP.NET Core project. Put files there when they do not need to be generated for every request, such as HTML, images, CSS stylesheets, and JavaScript files. Dynamic content, by contrast, is generated in response to a user's identity or actions. section 15.4

To serve a static HTML file:

  • Create wwwroot in the project and add demo.html. Give the file its .html extension when creating it manually. section 4.2
  • For the minimal application, add app.UseStaticFiles() to Program.cs. This enables responses using files in wwwroot. section 4.3
  • Run the application and request http://localhost:5000/demo.html to see the file in a browser. section 4.3
Listing 4.4 Adding a statement in the Program.cs file in the MyProject folder
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/", () => "Hello World!");

app.UseStaticFiles();

app.Run();

The Program.cs listing shows where app.UseStaticFiles() fits in the minimal application. section 4.3

A stylesheet for validation errors

Place styles.css in wwwroot/css and link to it with href="/css/styles.css". The URL omits wwwroot: in the configured application, requests for static content map to that folder. The stylesheet can highlight inputs marked with input-validation-error after a validation failure. section 3.2.9 section 3.2.9

Do not confuse the folder convention with the setup needed to serve its files. The minimal application adds app.UseStaticFiles() explicitly; the configured validation example already supports static-content requests. section 4.3 section 3.2.9

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

How deep it goes
Shows folder creation, static-file configuration, and browser verification, but defers fuller treatment of static-file serving.
Examples
Uses demo.html at /demo.html and a validation stylesheet stored at wwwroot/css/styles.css.
What it assumes
Starts with an ASP.NET Core project and walks through creating the folder and HTML file rather than requiring them to exist.
What you can do after
Operate/configure it by adding a static file, enabling serving when needed, and checking its browser URL.
How it connects
Connects static files to HTTP requests, CSS stylesheets, and form validation styling.

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 Pro ASP.NET Core 7, Tenth Edition. The section links point to the parts of the book it draws on.