Static Files Middleware in ASP.NET Core

Static Files Middleware in ASP.NET Core Application

In this article, I will discuss how to Serve Static Files using Static Files Middleware in an ASP.NET Core Web Application with Examples. Please read our previous article before proceeding to this one, where we discussed the wwwroot folder in the ASP.NET Core Application. As part of this article, we will discuss the following pointers in detail.

  1. Where do we need to store the static files in ASP.NET Core?
  2. What is wwwroot folder in ASP.NET Core?
  3. How do you configure static files middleware in the ASP.NET Core Web Application?
  4. How to use your own Webroot folder?
Static Files Middleware in ASP.NET Core Web Application:

One of the most important features of almost all web applications should be the ability to serve static files directly from the file system. Static files such as HTML, Images, CSS, and JavaScript are the important assets of a Web Application, and ASP.NET Core can serve these files directly to the clients. However, the important point you need to remember is that ASP.NET Core cannot serve these static files by default. Some configuration is required to enable ASP.NET Core to serve these static files directly. The Static Files Middleware is responsible for handling static files and making them accessible to clients.

In the ASP.NET Core Application, the default directory or location for the static files is the wwwroot (webroot) folder, which should be present in the project root directory. By default, this is the only place where the ASP.NET Core application can serve the static files directly. However, we can change this default behavior using the WebApplicationOptions Instance and WebRootPath property.

Example to understand Static Files in .NET Application:

When we create a new ASP.NET Core Application with the Empty project template, by default, you will not find the wwwroot folder. The project structure of the ASP.NET Core Web Application with the Empty Project template is shown below.

ASP.NET Core Main Method in .NET 6

As you can see in the above image, our application does not have a folder called wwwroot. However, if you create the Project using the MVC (Model-View-Controller) Project Template, Visual Studio will create the wwwroot folder by default.

Adding the wwwroot (webroot) Folder:

Let us first create the wwwroot Project Folder. To do so, right-click on the project, select the add => new folder option from the context menu, and then provide the folder name as wwwroot. Once you create the wwwroot folder, your project structure should be shown below.

Adding wwwroot (webroot) Folder in ASP.NET Core

Once you have created the wwwroot folder, add an Image file. Please download and paste the following image into the wwwroot folder and modify the image name as MyImage.png.

Dot Net Tutorials Logo

Once you save the above image, your wwwroot directory looks as shown below.

Adding Image to the wwwroot (webroot) Folder

Now run the application and navigate to the following URL. You need to replace the port number on which your application is running. 

http://localhost:<portnumber>/MyImage.png

When you navigate to the above URL, you will not get the output as expected; rather, you will get the following output.

ASP.NET Core Static Files Middleware

We are not getting the output as expected because we don’t have any middleware that can serve the static files in the request processing pipeline.

How Do We Configure the Static Files Middleware in ASP.NET Core Application?

To handle the static resources in the ASP.NET Core Web Application, we need to configure a middleware called UseStaticFiles() into the application Request Processing Pipeline. The UseStaticFiles() middleware is an inbuilt Middleware provided by the ASP.NET Core Framework to handle the static files in an ASP.NET Core Web Application.

Let us Modify the Main() Method of the Program class, as shown below, to register the UseStaticFiles() Middleware Component in the application’s Request Processing Pipeline. This method should be placed before any middleware that requires access to the static files (such as MVC middleware if you’re using it).

namespace FirstCoreWebApplication
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var builder = WebApplication.CreateBuilder(args);

            var app = builder.Build();

            //Adding Static Files Middleware Component to serve the static files
            app.UseStaticFiles();

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

            //This will Run the Application
            app.Run();
        }
    }
}

With the above changes in place, now run the application and navigate to the URL: http://localhost:<portnumber>/MyImage.png, and you will see the output as expected, as shown in the below image.

How to Configuring the Static Files Middleware in ASP.NET Core Application

How Do We Create Our Own Webroot Folder in ASP.NET Core?

Let’s say we don’t want wwwroot as our webroot folder; instead, we want MyWebRoot as the webroot folder for our application. First, modify the wwwroot folder as MyWebRoot, and once you modify it, your project structure should be as shown below.

How to Create your own Webroot Folder in ASP.NET Core

At this point, if you run the application, then you will not get the output as shown in the below image.

ASP.NET Core Static Files Middleware

This is because, by default, the static files middleware will look for a folder named wwwroot that is not present in our application. But we don’t want wwwroot. We want the Static files middleware to look at the MyWebRoot folder to serve the static files such as CSS, Images, JS, etc. To do so, we need to tell the ASP.NET Core Framework to use MyWebRoot as the web root path. So, we need to set the WebRootPath property to MyWebRoot while creating the WebApplicationBuilder instance. Please modify the Main method of the Program class as shown below to configure the MyWebRoot folder as the webroot folder for our application.

namespace FirstCoreWebApplication
{
    public class Program
    {
        public static void Main(string[] args)
        {
            //var builder = WebApplication.CreateBuilder(args);

            //Setting Custom Web Root Folder
            WebApplicationBuilder builder = WebApplication.CreateBuilder(new WebApplicationOptions
            {
                WebRootPath = "MyWebRoot"
            });

            var app = builder.Build();

            //Adding Static Files Middleware Component to serve the static files
            app.UseStaticFiles();

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

            //This will Run the Application
            app.Run();
        }
    }
}

After making the above changes, run the application, and you should get the expected output, as shown in the image below.

How to Configuring the Static Files Middleware in ASP.NET Core Application

In the next article, we will discuss Configuring the Default Page in the ASP.NET Core Application with Examples. In this article, I explain how to serve static files using the static files middleware component in the ASP.NET Core Web application. I would like to have your feedback about this article. Please post your feedback, questions, or comments about this Static Files Middleware Component in the ASP.NET Core Web Application article.

4 thoughts on “Static Files Middleware in ASP.NET Core”

  1. blank

    It’s missing parameter defaultFilesOptions in app.UseDefaultFiles(), if you want to display MyCustomPage1.html instead of default index.html…

    //Adding Default Files Middleware to set the default page
    app.UseDefaultFiles();

Leave a Reply

Your email address will not be published. Required fields are marked *