ASP.NET Core 1.0: Middleware And Static files (Part 2)

Introduction

We will continue the second part of Middleware And Static files in ASP.NET Core 1.0. Before reading this article, you must read our previous article ASP.NET Core 1.0: Middleware And Static files (Part 1) because we have explained some important parts in our previous article. In this article, I will teach you the remaining part of Middleware & Static files 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, a library for the path & directory libraries and other libraries.

using System.IO;  
using Microsoft.Extensions.FileProviders;

ASP.NET Core 1.0 StartUp Class

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Threading.Tasks;  
using Microsoft.AspNetCore.Builder;  
using Microsoft.AspNetCore.Hosting;  
using Microsoft.AspNetCore.Http;  
using Microsoft.Extensions.DependencyInjection;  
using Microsoft.Extensions.Logging;  
using Microsoft.Extensions.FileProviders;  
using System.IO;  
  
namespace DotnetCore  
{  
 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 http://go.microsoft.com/fwlink/?LinkID=398940 ; 
 public void  ConfigureServices(IServiceCollection services)  
 {  
 services.AddDirectoryBrowser();  
 }  
  
 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.  
 public void  Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)  
 {  
 loggerFactory.AddConsole();  
  
 if (env.IsDevelopment())  
 {  
 app.UseDeveloperExceptionPage();  
 }  
  
 app.UseStaticFiles(); // wwwroot inside file access.  
 app.UseDefaultFiles(); // Default startup file of app.  
 //app.UseWelcomePage(); // Welcome Page.  
 app.UseStaticFiles(new StaticFileOptions()  
 {  
 FileProvider = new  PhysicalFileProvider(  
 Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot","StaticFiles")),  
 RequestPath = new  PathString("/InsideRoot") // accessing wwwroot inside folder contents.   
 });  
  
 app.UseStaticFiles(new StaticFileOptions()  
 {  
 FileProvider = new  PhysicalFileProvider(  
 Path.Combine(Directory.GetCurrentDirectory(), @"StaticFiles","Home")),  
 RequestPath = new  PathString("/OutsideRoot") // accessing outside wwwroot folder contents.   
 });  
 app.UseDirectoryBrowser(new DirectoryBrowserOptions()  
 {  
 FileProvider = new  PhysicalFileProvider(  
 Path.Combine(Directory.GetCurrentDirectory(), @"StaticFiles", "Home")),   
 RequestPath = new  PathString("/Directory") // listing directory details for specified folder.  
 });  
  
 app.UseStaticFiles(new StaticFileOptions()  
 {  
 FileProvider = new  PhysicalFileProvider(  
 Path.Combine(Directory.GetCurrentDirectory(), @"StaticFiles", "Home")),   
 RequestPath = new  PathString("/Directory") // accessing listed directory contents.   
 });  
 app.Run(async (context) =>  
 {  
 await context.Response.WriteAsync("Hello World!");  
 });  
 }  
 }  
}

Static files are Inside and Outside of wwwroot or Webroot

In the project structure given below, Static files 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.

app.UseStaticFiles(new StaticFileOptions()  
 {  
 FileProvider = new  PhysicalFileProvider(  
 Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot","StaticFiles")),  
 RequestPath = new  PathString("/InsideRoot") // accessing wwwroot inside folder contents.   
 });

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.

app.UseStaticFiles(new StaticFileOptions()  
 {  
 FileProvider = new  PhysicalFileProvider(  
 Path.Combine(Directory.GetCurrentDirectory(), @"StaticFiles","Home")),  
 RequestPath = new  PathString("/OutsideRoot") // accessing outside wwwroot folder contents.   
 });

Output

Directory browsing in ASP.NET Core 1.0

It allows the user of our 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.

app.UseDirectoryBrowser(new DirectoryBrowserOptions()  
 {  
 FileProvider = new  PhysicalFileProvider(  
 Path.Combine(Directory.GetCurrentDirectory(), @"StaticFiles", "Home")),   
 RequestPath = new  PathString("/Directory") // listing directory details for specified folder.  
 });

Output

Accessing Directory browsing contents in ASP.NET Core 1.0

If we want to access the directory contents in ASP.NET Core 1.0, add the same path string in "UseStaticFiles" extension method.

app.UseDirectoryBrowser(new DirectoryBrowserOptions()  
 {  
 FileProvider = new  PhysicalFileProvider(  
 Path.Combine(Directory.GetCurrentDirectory(), @"StaticFiles", "Home")),   
 RequestPath = new  PathString("/Directory") // listing directory details for specified folder.  
 });  
  
app.UseStaticFiles(new StaticFileOptions()  
 {  
 FileProvider = new  PhysicalFileProvider(  
 Path.Combine(Directory.GetCurrentDirectory(), @"StaticFiles", "Home")),   
 RequestPath = new  PathString("/Directory") // accessing listed directory contents.   
 });

Reference

Back to top

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.