Menu Close

How to rename wwwroot folder name in ASP.NET Core


In this article we discuss about How to rename wwwroot folder name in ASP.Net Core. 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. To Find more details about it, please go through our previous article ASP.NET Core environment variables.

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.

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()

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();
        }
rename wwwroot

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

wwwroot middleware

You can see the below Video

Conclusion

Leave behind your valuable queries, suggestions in the comment section below. Also, if you think this article help to you, do not forget to share this within your developer community. Happy Coding 🙂

Jayant Tripathy
Coder, Blogger, YouTuber

A passionate developer keep focus on learning and working on new technology.


Leave a Reply

Your email address will not be published.