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.
The preceding namespace contains PhysicalFileProvider, PathString, a library for the path & directory libraries and other libraries.
using
System.IO;
Microsoft.Extensions.FileProviders;
System;
System.Collections.Generic;
System.Linq;
System.Threading.Tasks;
Microsoft.AspNetCore.Builder;
Microsoft.AspNetCore.Hosting;
Microsoft.AspNetCore.Http;
Microsoft.Extensions.DependencyInjection;
Microsoft.Extensions.Logging;
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 ;
void
ConfigureServices(IServiceCollection services)
services.AddDirectoryBrowser();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
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 =
PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), @
"wwwroot"
,
"StaticFiles"
)),
RequestPath =
PathString(
"/InsideRoot"
)
// accessing wwwroot inside folder contents.
});
"Home"
"/OutsideRoot"
// accessing outside wwwroot folder contents.
app.UseDirectoryBrowser(
DirectoryBrowserOptions()
"/Directory"
// listing directory details for specified folder.
// accessing listed directory contents.
app.Run(async (context) =>
await context.Response.WriteAsync(
"Hello World!"
);
In the project structure given below, Static files are placed inside and outside of the 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.
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.
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.
If we want to access the directory contents in ASP.NET Core 1.0, add the same path string in "UseStaticFiles" extension method.
Back to top
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.