ASP.NET core – Overview of Static files

facebooktwittergoogle_plusredditpinterestlinkedinmail

Static file in ASP.NET core- feature

In the previous asp.net article we have seen the new tag helper which is introduced in ASP.NET core (formally ASP.NET 5) . In this article I will explain about WWWroot folder, Directory browsing and static files in ASP.NET core.

WWWroot folder in ASP.NET Core

WWWroot is the root folder of the ASP.NET core which contains all the static files in the project. Static files include HTML file, CSS files , javascript files and images. The code files should be placed outside the wwwroot folder . C# file and Razor view will be stored outside the wwwroot folder.

Static files in ASP.NET core - wwwroot folder

Manage static files in ASP.NET Core

This static file can be accessed from the URL. We can configure the static file in startup.cs class (middleware). Create an ASP.NET project with ASP.NET 5 template. Run the application you will get nice ASP.NET core web site .

Static file in ASP.NET core - enable static files

Navigate the startup.cs file in the solution explorer , then you can see one extension method called usestaticfiles(). This method is responsible to serve the static files in the application .

app.UseStaticFiles()

Comment the preceding line of code in the startup.cs file and run the application . The web page will come without any style, script and images. Sometimes the browser will take the page from the cache so you have to clear the browser cache before run the application . If you are serving static file from the CDN (Content Delivery Network) then go ahead and turn off the static files. In the following screen you can see the page without static contents.

Static file in ASP.NET core - disable static files

These static files can be accessed via URL.

 http://localhost:62875/css/site.css

Serve static files from outside WWWroot folder

We can even access the static files that are not under WWWrooot folder . UseStaticFiles() method has different option to do this. Suppose I have one text file ( textfile.txt ) in the view folder, then UseStaticeFiles() method should be like

 app.UseStaticFiles(new StaticFileOptions()
            {
 
                FileProvider = new PhysicalFileProvider(@"C:\Users\Tom\documents\visual studio 2015\Projects\WebApplication3\src\WebApplication3\Views\Account\"),
                RequestPath = new PathString("/myfile")
            });

Then the file can be access using the following URL

 http://localhost:62875/myfile/textfile.txt

Directory browsing in ASP.NET Core

Directory browsing allows the user of your Web app to see a list of directories and files within a specified directory (including the root).This feature is disabled by default . You need to call UseDirectoryBrowser() method to enable directory browsing from the middleware ( startup.cs ).

 app.UseDirectoryBrowser();

Add the preceding line of code and run the application then you will be able to navigate the directories through the browser.

Static file in ASP.NET core - Directory Browsing

The following two tabs change content below.

Tom Mohan

Tom Mohan is a technologist who loves to code and build. He enjoys working on Microsoft Technologies. Tom specializes in ASP.NET MVC, Web API , Azure, C# ,WPF, SQL etc and holds a Bachelor engineering degree in Computer Science. Certification : MCSD , MCTS
  • Zachary Gover

    Would you mind showing us some details about enabling static files within other folders, such as the “Views” folder?

    • DotnetReference.com

      Thank you for you valuable suggestion. We have added the answer in the ‘Serve static files from outside WWWroot folder ‘ Section .