Tutorials ASP.NET Core MVC Tutorial
Static Files Middleware — Complete Guide
Static Files Middleware — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of ASP.NET Core MVC Tutorial on Toolliyo Academy.
On this page
ASP.NET Core MVC Tutorial · Lesson 27 of 200
Static Files Middleware
Getting Started ✓ → Core MVC → Data & Security → Production → Career
Beginner · 3 — Controllers & Views · ~6 min · Section 2: ASP.NET Core Basics & Hosting
What is this?
Static files middleware serves files from wwwroot (and optional extra folders) with correct content types — bypassing controllers for speed.
Why should you care?
Every page needs CSS and JS. Serving them efficiently reduces server load and improves load time.
See it live — copy this example
Create an MVC project (dotnet new mvc), add the code, and run dotnet run.
app.UseStaticFiles();
// Optional — second folder
app.UseStaticFiles(new StaticFileOptions {
FileProvider = new PhysicalFileProvider(
Path.Combine(builder.Environment.ContentRootPath, "Uploads")),
RequestPath = "/uploads"
});
Run Example »
Edit the code and click Run — like W3Schools Try it Yourself.
What happened?
- UseStaticFiles must appear before routing in typical setups.
- Default cache headers can be tuned for production CDN.
Try it yourself
- Confirm UseStaticFiles in Program.cs.
- Request a missing file — 404 from static middleware.
- Add cache-control headers in production config if needed.
- Change text or labels in the example and run again — watch the browser update.
- Break the code on purpose (remove a semicolon), read the error message, then fix it.
Remember
UseStaticFiles enables wwwroot. Runs early in the pipeline. Can map extra folders with RequestPath.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!