Skip to main content

How to Serve Static Files in ASP.NET Core with MapStaticAssets (C#)

· 8 min read
Jagdish Kumawat
Founder @ Dewiride

A new ASP.NET Core app answers only the routes in its code, so a page you put in its wwwroot folder comes back as 404 Not Found. One line, app.MapStaticAssets(), serves every file in that folder, with its content type and caching headers already set.

Syntax​

Program.cs
app.MapStaticAssets();
PartWhat it does
appThe WebApplication that builder.Build() returns
MapStaticAssets()Serves the files under wwwroot that the build found, each at its own address

A file's address is its path inside wwwroot: wwwroot/hello.html is served at /hello.html, and wwwroot/css/site.css at /css/site.css. The folder name itself isn't part of the address.

Microsoft's static files guide starts with exactly this: To enable static file handling in ASP.NET Core, call MapStaticAssets. It also explains where the list of files comes from: MapStaticAssets serves assets from a static assets manifest rather than by scanning the web root at runtime. The manifest is generated at build and publish time. The build looks at wwwroot and writes down every file it finds, and the app serves what's on that list.

The Example​

An empty web app, StaticFilesDemo, gets one page, wwwroot/hello.html, that says Hello from a static file! The app runs first without MapStaticAssets and then with it, and curl shows what each answer looks like.

Prerequisites​

  • The .NET 10 SDK. Check with dotnet --version. This guide used 10.0.404.

Step 1: Create an Empty Web App​

Terminal
dotnet new web -n StaticFilesDemo
cd StaticFilesDemo

dotnet new web creates the smallest ASP.NET Core app there is, from the ASP.NET Core Empty template, and -n names the project and its folder:

Output
The template "ASP.NET Core Empty" was created successfully.

Processing post-creation actions...
Restoring /Users/you/StaticFilesDemo/StaticFilesDemo.csproj:
Determining projects to restore...
Restored /Users/you/StaticFilesDemo/StaticFilesDemo.csproj (in 49 ms).
Restore succeeded.

Its Program.cs answers one route, /, with Hello World!:

Program.cs
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

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

app.Run();

The template doesn't create a wwwroot folder. You'll add it next.

Step 2: Add a Page to wwwroot​

Create a folder named wwwroot inside StaticFilesDemo, and in it a file named hello.html:

wwwroot/hello.html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Hello</title>
</head>
<body>
<p>Hello from a static file!</p>
</body>
</html>

wwwroot is ASP.NET Core's web root: the one folder whose files an app can hand out to browsers. Files anywhere else in the project, such as appsettings.json, are never served.

Step 3: Run It Without MapStaticAssets​

Start the app:

Terminal
dotnet run
Output
Using launch settings from /Users/you/StaticFilesDemo/Properties/launchSettings.json...
Building...
info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://localhost:5088
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
Content root path: /Users/you/StaticFilesDemo

The template picks a random port for each new project, so yours will differ: use the one on the Now listening on line. In a second terminal, ask for the page. curl sends the request, and -i prints the response's headers above its body:

Terminal
curl -i http://localhost:5088/hello.html
Output
HTTP/1.1 404 Not Found
Content-Length: 0
Date: Thu, 03 Dec 2026 08:54:31 GMT
Server: Kestrel

The file is there, but nothing in Program.cs serves it. In PowerShell, typing curl.exe rather than curl makes sure you get the real curl, which Windows 10 and 11 include, instead of the curl alias that Windows PowerShell 5.1 has for Invoke-WebRequest.

Stop the app with Ctrl+C.

Step 4: Add MapStaticAssets​

Add one line to Program.cs, above the MapGet line:

Program.cs in a macOS-style code window: WebApplication.CreateBuilder, builder.Build, app.MapStaticAssets(), app.MapGet for the root path returning Hello World!, and app.Run.

Program.cs
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

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

app.Run();

Run the app again with dotnet run. dotnet run builds the project first, and that build is what lists hello.html among the files to serve. Then ask for the page again:

Output
HTTP/1.1 200 OK
Content-Length: 154
Content-Type: text/html
Date: Thu, 03 Dec 2026 08:54:36 GMT
Server: Kestrel
Accept-Ranges: bytes
Cache-Control: no-cache
ETag: "5L4Nh3v32q7Ri18FIyp5e0cVJOqGkS/xCaDOD3tg3Sk="
Last-Modified: Thu, 03 Dec 2026 08:39:17 GMT
Vary: Accept-Encoding

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Hello</title>
</head>
<body>
<p>Hello from a static file!</p>
</body>
</html>

The headers came with it, without any code for them:

  • Content-Type: text/html comes from the .html extension, so the browser shows the file as a page.
  • ETag is a fingerprint of the file's content, and Last-Modified is when the file last changed. A browser sends them back on its next visit, and gets the whole file again only if it changed.
  • Cache-Control: no-cache tells the browser to check with the app before it reuses its copy, which is what you want while you're still editing the file.

Open http://localhost:5088/hello.html in a browser, with your port:

A browser page showing the text Hello from a static file!

A File Added While the App Runs​

The list of files comes from the build, so a file you add to wwwroot while the app runs isn't on it. In Development, MapStaticAssets still serves such a file, and writes a warning in the app's terminal:

Output
warn: Microsoft.AspNetCore.Builder.StaticAssetDevelopmentRuntimeHandler[1]
The static asset '/later.html' was not found in the built time manifest. This file will not be available at runtime if it is not available at compile time during the publish process. If the file was not added to the project during development, and is created at runtime, use the StaticFiles middleware to serve it instead.

Stopping the app and running it again puts the file on the list, and the warning goes away. Fixed: The static asset was not found in the built time manifest in ASP.NET Core shows that run step by step.

Common Mistakes​

MistakeWhat happensDo this instead
Putting the file next to Program.cs instead of in wwwroot404: only wwwroot is servedMove it into wwwroot
Putting wwwroot in the address, as in /wwwroot/hello.html404Leave the folder name out: /hello.html
Expecting / to show wwwroot/index.htmlMapStaticAssets doesn't serve default documentsAsk for /index.html, or see the FAQ below
Adding MapStaticAssets while the app runsThe running app doesn't changeStop it with Ctrl+C and run dotnet run again

Troubleshooting​

SymptomLikely causeFix
404 Not Found for a file in wwwrootNo app.MapStaticAssets() in Program.csAdd it before app.Run(), then restart
curl: (7) Failed to connectThe app isn't running, or it's on another portStart it, and use the port from Now listening on
A warning that the file was not found in the built time manifestThe file was added after the buildRestart the app

FAQ​

Do I still need UseStaticFiles?​

Not for files in wwwroot. The guide says Use Map Static Assets endpoint conventions in apps that target .NET 9 or later. app.UseStaticFiles() is still the tool for what MapStaticAssets doesn't do, such as files outside wwwroot, files created while the app runs, and default documents: the guide's example for those is app.UseDefaultFiles(); app.UseStaticFiles(); app.MapStaticAssets();.

Can MapStaticAssets add my own headers to the files?​

No. The guide lists Set HTTP response headers among the features of the static file middleware, not of MapStaticAssets. To add a header to every response, static files included, use a middleware of your own, as in How to Add a Content-Security-Policy Header in ASP.NET Core.

Conclusion​

app.MapStaticAssets() serves the files the build found in wwwroot, each at its path inside that folder, with Content-Type, ETag, Last-Modified and Cache-Control set for you. Without it, those files are 404s. Restart the app after adding a file, so the build can list it.

Additional Resources​