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 that only can be served over an http request. All other files are blocked and cannot be served by default.

By default, the wwwroot folder in the ASP.NET Core application is treated as a web root folder. The static files are recommended to store in different folders like images are in “images“, css files are into “css” folders and Javascript files are in “js” folder like in the below image.

CoreSpider - WWWroot

You can access static files with base URL and file name. For example, we can access above image1.png file in the images folder by http://localhost:port/css/image1.png

To serve the static files you need to add the middleware.

app.UseStaticFiles()

To Find more details about it, please go through our previous article Static files in ASP .NET Core.

Rename wwwroot Folder

To rename the wwwroot folder we need to change in program.cs file. You can rename it to as per your convenient according to your project.

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

public static void Main(string[] args)
        {
            var host = new WebHostBuilder()
                       .UseKestrel()
                       .UseWebRoot("myCustomRoot")
                       .UseContentRoot(Directory.GetCurrentDirectory())
                       .UseIISIntegration()
                       .UseStartup<Startup>()
                       .Build();
               host.Run();
        }
CoreSpider - app.UseStaticFiles()

After doing this now it enable to serve as myCustomRoot is to serve the static file. Run the application

CoreSpider - Custom root path

Video Summary

Conclusion

This article explain how to rename the wwwroot folder and add your custom folder. If have any doubt or concern about this article then please give your feedback.

Pin It on Pinterest

Share This