What Is wwwroot Folder in Asp.Net Core

Introduction :

By default, the wwwroot folder in the ASP.NET Core application is treated as the webroot folder and this folder should be present in the root project folder. In ASP.NET Core Application, the Static files can be stored in any folder under the webroot folder and can be accessed with a relative path to that root.

In the standard ASP.NET application, static files can be served from the root folder of an application or any other folder under it. This has been changed in ASP.NET Core. Now, only those files that are in the web root – wwwroot folder can be served over an http request. All other files are blocked and cannot be served by default.

Generally, there should be separate folders for the different types of static files such as JavaScript, CSS, Images, library scripts etc. in the wwwroot folder as shown below.

Adding wwwroot (webroot) folder in ASP.NET Core

Asp.Net Core wwwroot folder

Now, you can access static files such as CSS, js, lib with base URL and file name. For example, you can access the above site.js file in the js folder by https://localhost:<port>/js/site.js

You can rename the wwwroot folder to any other name as per your choice and set it as a webroot while preparing the hosting environment in the Program.cs file.

For example, let’s rename wwwroot folder to Content folder. Now, call UseWebRoot() method to configure MyWebRoot folder as a web root folder in the Main() method of Program class as shown below.

    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup().UseWebRoot("MyWebRoot");
                });
    }

With the above changes in place, now the MyWebRoot folder is going to act as the Webroot folder for your application and can serve the static files HTTP requests.

SEE MORE ARTICLE

Leave a Reply

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