Web
Analytics
asp.net core 2.2 static files | asp.net core 2.2 tutorial | Angular | ASP.NET Tutorials

For Consultation : +91 9887575540

Stay Connected :

asp.net core 2.2 static files

As per best programming practices, we should not use a folder other than wwwroot for static files like images, CSS, js, HTML pages, etc. The wwwroot is the standard static directory in ASP.NET Core, and other developers who dive into your code will have an easier time understanding it if you stick to common practices.

In this blog, we will see how we can configure static files and folders, Let’ see a simple example of the configuration in the startup.cs class.

namespace Core_Basic
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
             // Default setting to access wwwroot
            app.UseStaticFiles();

            // If we are using different folders then we have to configure like this
            // create new folder myfile and add index2.html
            // access url for it will be http://localhost:1245/files/index2.html
            // Now If we place only it then wwwroots will not work so individual path has to mentioned
            app.UseStaticFiles(new StaticFileOptions
            {
                FileProvider = new PhysicalFileProvider (
                    
                    Path.Combine(Directory.GetCurrentDirectory(), "myfile")), RequestPath =new PathString("/files")
            });
           // when we set single file options then we have to write to all for example write for wwwroots
            app.UseStaticFiles(new StaticFileOptions
            {
                FileProvider = new PhysicalFileProvider(

                   Path.Combine(Directory.GetCurrentDirectory(), "wwwroots")),
                RequestPath = new PathString("/main")
            });
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.Run(async (context) =>
            {
                await context.Response.WriteAsync("Hello World!");
            });
        }
    }
}




Detailed Description: 

app.UseStaticFiles(); middleware is called to configure wwwroot folder. If we created an HTML page in the wwwroot folder and trying to access that HTML page from URL then It will not work without this middleware configuration so It is mandatory to declare in Configure method of Startup.cs class.if you are not familiar to Configure method and asp.net core 2.2 folder structure then click on this link.

In any situation we want to use another folder other than wwwroot then we have to configure that using the following configuration. But one thing has to keep in our mind that If we are using another folder for static material along with wwwroot then the configuration for the custom folder and wwwroot folder is necessary.

            app.UseStaticFiles(new StaticFileOptions
            {
                FileProvider = new PhysicalFileProvider (
                    
                    Path.Combine(Directory.GetCurrentDirectory(), "myfile")), RequestPath =new PathString("/files")
            });
            app.UseStaticFiles(new StaticFileOptions
            {
                FileProvider = new PhysicalFileProvider(

                   Path.Combine(Directory.GetCurrentDirectory(), "wwwroots")),
                RequestPath = new PathString("/main")
            });

asp.net core 2.0 startup page

  • Add index.html in the wwwroot folder to make index.html as default file as the startup of the application.
  • Use Default Files method ( app.UseDefaultFiles();)  must be called before use static files middlwware method(app.UseStaticFiles();).

To change the default file name from index.html to main.html

// Create main.html into wwwroot folder
            DefaultFilesOptions options = new DefaultFilesOptions();
            options.DefaultFileNames.Clear();
            options.DefaultFileNames.Add("main.htm");
            options.DefaultFileNames.Add("main.html");
            app.UseDefaultFiles(options);