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.
- 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
- {
-
-
- public void ConfigureServices(IServiceCollection services)
- {
- services.AddDirectoryBrowser();
- }
-
-
- public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
- {
- loggerFactory.AddConsole();
-
- if (env.IsDevelopment())
- {
- app.UseDeveloperExceptionPage();
- }
-
- app.UseStaticFiles();
- app.UseDefaultFiles();
-
- app.UseStaticFiles(new StaticFileOptions()
- {
- FileProvider = new PhysicalFileProvider(
- Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot","StaticFiles")),
- RequestPath = new PathString("/InsideRoot")
- });
-
- app.UseStaticFiles(new StaticFileOptions()
- {
- FileProvider = new PhysicalFileProvider(
- Path.Combine(Directory.GetCurrentDirectory(), @"StaticFiles","Home")),
- RequestPath = new PathString("/OutsideRoot")
- });
- app.UseDirectoryBrowser(new DirectoryBrowserOptions()
- {
- FileProvider = new PhysicalFileProvider(
- Path.Combine(Directory.GetCurrentDirectory(), @"StaticFiles", "Home")),
- RequestPath = new PathString("/Directory")
- });
-
- app.UseStaticFiles(new StaticFileOptions()
- {
- FileProvider = new PhysicalFileProvider(
- Path.Combine(Directory.GetCurrentDirectory(), @"StaticFiles", "Home")),
- RequestPath = new PathString("/Directory")
- });
- app.Run(async (context) =>
- {
- await context.Response.WriteAsync("Hello World!");
- });
- }
- }
- }
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.
- app.UseStaticFiles(new StaticFileOptions()
- {
- FileProvider = new PhysicalFileProvider(
- Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot","StaticFiles")),
- RequestPath = new PathString("/InsideRoot")
- });
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")
- });
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.
- app.UseDirectoryBrowser(new DirectoryBrowserOptions()
- {
- FileProvider = new PhysicalFileProvider(
- Path.Combine(Directory.GetCurrentDirectory(), @"StaticFiles", "Home")),
- RequestPath = new PathString("/Directory")
- });
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.
- app.UseDirectoryBrowser(new DirectoryBrowserOptions()
- {
- FileProvider = new PhysicalFileProvider(
- Path.Combine(Directory.GetCurrentDirectory(), @"StaticFiles", "Home")),
- RequestPath = new PathString("/Directory")
- });
-
- app.UseStaticFiles(new StaticFileOptions()
- {
- FileProvider = new PhysicalFileProvider(
- Path.Combine(Directory.GetCurrentDirectory(), @"StaticFiles", "Home")),
- RequestPath = new PathString("/Directory")
- });
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.