Middleware And Staticfiles In ASP.NET Core 1.0 - Part Two

Introduction 
 
We will continue the second part of Middleware And Staticfiles in ASP.NET Core 1.0. Before reading this article, you must read my previous article Middleware And Staticfiles In ASP.NET Core 1.0 - Part One because I have explained some important parts in my previous article. In this article, I will teach you the remaining part of Middleware & Staticfiles in ASP.NET Core 1.0. 
 
Before reading this article, you must read the articles given below for ASP.NET Core knowledge.
Assemblies required
 
The preceding namespace contains PhysicalFileProvider, PathString, library for the path & directory libraries and other libraries.
  1. using System.IO;  
  2. using Microsoft.Extensions.FileProviders;  
ASP.NET Core 1.0 StartUp Class
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Threading.Tasks;  
  5. using Microsoft.AspNetCore.Builder;  
  6. using Microsoft.AspNetCore.Hosting;  
  7. using Microsoft.AspNetCore.Http;  
  8. using Microsoft.Extensions.DependencyInjection;  
  9. using Microsoft.Extensions.Logging;  
  10. using Microsoft.Extensions.FileProviders;  
  11. using System.IO;  
  12.    
  13. namespace DotnetCore  
  14. {  
  15.     public class Startup  
  16.     {  
  17.         // This method gets called by the runtime. Use this method to add services to the container.  
  18.         // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940  
  19.         public void ConfigureServices(IServiceCollection services)  
  20.         {  
  21.             services.AddDirectoryBrowser();  
  22.         }  
  23.    
  24.         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.  
  25.         public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)  
  26.         {  
  27.             loggerFactory.AddConsole();  
  28.    
  29.             if (env.IsDevelopment())  
  30.             {  
  31.                 app.UseDeveloperExceptionPage();  
  32.             }  
  33.                
  34.             app.UseStaticFiles(); // wwwroot inside file access.  
  35.             app.UseDefaultFiles(); // Default startup file of app.  
  36.             //app.UseWelcomePage(); // Welcome Page.  
  37.             app.UseStaticFiles(new StaticFileOptions()  
  38.             {  
  39.                 FileProvider = new PhysicalFileProvider(  
  40.            Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot","StaticFiles")),  
  41.                 RequestPath = new PathString("/InsideRoot"// accessing wwwroot inside folder contents.  
  42.             });  
  43.    
  44.             app.UseStaticFiles(new StaticFileOptions()  
  45.             {  
  46.                 FileProvider = new PhysicalFileProvider(  
  47.             Path.Combine(Directory.GetCurrentDirectory(), @"StaticFiles","Home")),  
  48.                 RequestPath = new PathString("/OutsideRoot"// accessing outside wwwroot folder contents.  
  49.             });  
  50.             app.UseDirectoryBrowser(new DirectoryBrowserOptions()  
  51.             {  
  52.                 FileProvider = new PhysicalFileProvider(  
  53.             Path.Combine(Directory.GetCurrentDirectory(), @"StaticFiles""Home")),  
  54.                 RequestPath = new PathString("/Directory"// listing directory details for specified folder.  
  55.             });  
  56.    
  57.             app.UseStaticFiles(new StaticFileOptions()  
  58.             {  
  59.                 FileProvider = new PhysicalFileProvider(  
  60.            Path.Combine(Directory.GetCurrentDirectory(), @"StaticFiles""Home")),  
  61.                 RequestPath = new PathString("/Directory"// accessing listed directory contents.  
  62.             });  
  63.             app.Run(async (context) =>  
  64.             {  
  65.                 await context.Response.WriteAsync("Hello World!");  
  66.             });  
  67.         }  
  68.     }  
  69. }  
Staticfiles are Inside and Outside of wwwroot or Webroot
 
In the project structure given below, Staticfiles are placed inside and outside of the wwwroot or Webroot.
 
 
 
Serving Staticfiles are Inside and Outside of wwwroot or Webroot
  • Accessing Inside folder Staticfiles contents in wwwroot or Webroot
  • Accessing Outside folder Staticfiles contents wwwroot or Webroot
  • Directory browsing in ASP.NET Core 1.0
  • Accessing Directory browsing contents in ASP.NET Core 1.0
Accessing Inside folder Staticfiles contents in wwwroot or Webroot
 
In the code given below, Staticfiles are placed inside the “StaticFiles” folder. We created the customized path string "/InsideRoot" to access Staticfiles. 
  1. app.UseStaticFiles(new StaticFileOptions()  
  2.             {  
  3.                 FileProvider = new PhysicalFileProvider(  
  4.            Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot","StaticFiles")),  
  5.                 RequestPath = new PathString("/InsideRoot"// accessing wwwroot inside folder contents.  
  6.             });  
Output
 


Accessing Outside folder Staticfiles contents wwwroot or Webroot
 
In the code given below, Staticfiles are placed outside the wwwroot or Webroot and placed inside the Home folder in StaticFiles folder. We created the customized path string "/OutsideRoot" to access Staticfiles.
  1. app.UseStaticFiles(new StaticFileOptions()  
  2.             {  
  3.                 FileProvider = new PhysicalFileProvider(  
  4.             Path.Combine(Directory.GetCurrentDirectory(), @"StaticFiles","Home")),  
  5.                 RequestPath = new PathString("/OutsideRoot"// accessing outside wwwroot folder contents.  
  6.             });  
Output 
 


Directory browsing in ASP.NET Core 1.0 
 
It allows the user of your Web app to see all the list of directories and files within a specified directory. Directory browsing is disabled by default for security reasons because it will show the secret files in the directory. To enable directory browsing in ASP.NET Core 1.0, call "UseDirectoryBrowser" extension method from Startup.Configure.

We created the customized path string "/Directory" to access the directory.
  1. app.UseDirectoryBrowser(new DirectoryBrowserOptions()  
  2.             {  
  3.                 FileProvider = new PhysicalFileProvider(  
  4.             Path.Combine(Directory.GetCurrentDirectory(), @"StaticFiles""Home")),  
  5.                 RequestPath = new PathString("/Directory"// listing directory details for specified folder.  
  6.             });  
Output
 
Accessing Directory browsing contents in ASP.NET Core 1.0
 
If you want to access the directory contents in ASP.NET Core 1.0, add the same path string in "UseStaticFiles" extension method.
  1. app.UseDirectoryBrowser(new DirectoryBrowserOptions()  
  2.             {  
  3.                 FileProvider = new PhysicalFileProvider(  
  4.             Path.Combine(Directory.GetCurrentDirectory(), @"StaticFiles""Home")),  
  5.                 RequestPath = new PathString("/Directory"// listing directory details for specified folder.  
  6.             });  
  7.    
  8. app.UseStaticFiles(new StaticFileOptions()  
  9.             {  
  10.                 FileProvider = new PhysicalFileProvider(  
  11.            Path.Combine(Directory.GetCurrentDirectory(), @"StaticFiles""Home")),  
  12.                 RequestPath = new PathString("/Directory"// accessing listed directory contents.  
  13.             });  
Reference
Conclusion
 
We learned Middleware & Staticfiles in ASP.NET Core 1.0 Part Two and I hope you liked this article. Please share your valuable suggestions and feedback.