Lesson 30 of 40 Frontend Intermediate 35 min

Working with Static Files

Learn how static files work in ASP.NET Core and how to serve CSS, JavaScript, images, and other front-end assets. Static files are essential for building websites that look good, respond to user interaction, and provide a modern browsing experience.

Part 1: What Are Static Files?

Static files are files that are sent directly to the browser without being generated dynamically by a controller or Razor view. Common examples include:

These files play an important role in the appearance and interactivity of your application.

Part 2: The wwwroot Folder

In ASP.NET Core, static files are usually stored in the wwwroot folder. This folder is the web root of the application, meaning its contents can be served directly to the browser.

wwwroot/
├── css/
├── js/
├── images/
└── lib/

Organizing files into subfolders makes the project easier to manage.

Part 3: Enabling Static File Middleware

To allow ASP.NET Core to serve static files, you must enable the static file middleware in Program.cs.

app.UseStaticFiles();

Without this line, files inside wwwroot will not be accessible from the browser.

Part 4: Linking CSS and JavaScript Files

Once your static files are in wwwroot, you can reference them in your layout or views.

<link rel="stylesheet" href="~/css/site.css" />
<script src="~/js/site.js"></script>

The tilde ~ represents the root of the web application.

Part 5: Using Images

Images are also stored inside wwwroot and can be used in views just like CSS and JavaScript files.

<img src="~/images/logo.png" alt="Site Logo" />

This allows you to add logos, banners, icons, and other visual elements to your pages.

Part 6: Why Static Files Matter

Static files are important because they provide the presentation and behavior layer of your site.

File Type Purpose
CSS Controls layout, colors, fonts, and design
JavaScript Adds interactivity and client-side behavior
Images Provides branding and visual content
Fonts Improves typography and branding consistency

Part 7: Static Files in the Student Project

In your Student CRUD application, static files can be used to improve both design and usability. For example:

This helps turn the project from a simple demo into a more professional application.

Part 8: Best Practices

Good management of static files keeps your project cleaner and easier to maintain.

Summary

Static files are a vital part of every ASP.NET Core web application. By understanding how to use the wwwroot folder and static file middleware, you can serve stylesheets, scripts, images, and other assets effectively. This gives your application the design and interactivity needed for a polished user experience.

VISUAL STUDIO 2026 MADE EASY
Recommended Book

VISUAL STUDIO 2026 MADE EASY

Build real applications with C#, VB.NET, Python, JavaScript, C++, and .NET 10. A practical companion for mastering Visual Studio 2026 step by step.