liveBook · Manning

Static file

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

Static content is web content served as stored files without being dynamically generated for each request.

A static file is content served without generating a fresh response for each request. Images, CSS stylesheets, JavaScript files, and HTML pages can all serve this role. Unlike a shopping cart or product detail generated from a user’s actions, the file’s contents do not depend on the request. In an ASP.NET Core project, the conventional home for these files is wwwroot. section 15.4

Put the file in wwwroot, then make sure the application can serve it. A minimal Program.cs can call app.UseStaticFiles() before app.Run(). This adds static-file handling to the HTTP request pipeline; simply creating a file is not the whole setup in that example. Some project templates already configure static-content support, so check the application rather than assuming every project starts alike. section 4.3 section 3.2.9

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 UseStaticFiles() is added in a minimal application. section 4.3

Check the URL, not just the folder

With static-file support enabled, a demo.html file in wwwroot can be checked at http://localhost:5000/demo.html after running the application. The URL does not include wwwroot. Likewise, a stylesheet at wwwroot/css/styles.css is linked as /css/styles.css. section 4.3 section 3.2.9

Static files also support pages whose main response is handled elsewhere. A form view can link to a CSS file that highlights validation errors. For an unhandled exception, UseExceptionHandler("/error.html") can direct the browser to an HTML file in wwwroot—provided static-file middleware is also in the pipeline. Keep that error page simple; an elaborate error handler can fail while handling the original failure. section 3.2.9 section 16.6.1

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

How deep it goes
Shows file placement, middleware setup, and browser verification without descending into static-file middleware internals.
Examples
Uses a browser-requested HTML page, a validation stylesheet, and an HTML error page.
What it assumes
Assumes basic familiarity with ASP.NET Core projects and HTTP requests while spelling out wwwroot placement and minimal middleware setup.
What you can do after
Operate/configure it by placing files in wwwroot, enabling UseStaticFiles() where needed, and checking their URLs.
How it connects
Connects static files to the request pipeline, CSS validation styling, and exception handling.

Also explained in

Compare how 6 books explain static file →

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.