Enable directory browsing in Dotnet Core

Generally directory browsing can be controlled from IIS, but in dot net core, it need to configure in middleware startup. below code snipped will help you to do this:

    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDirectoryBrowser();
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseStaticFiles();

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

            app.UseDirectoryBrowser(new DirectoryBrowserOptions()
            {
                FileProvider = new PhysicalFileProvider(
              Path.Combine(Directory.GetCurrentDirectory(), @"/staticfileDir")),
                RequestPath = new PathString("/staticfileDir")
            });
        }
    }

Then run the program and specify it in the URLstaticfileDirPath, you will see staticfileDirAll contents under the folder.

Leave a Reply

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