Advertisements

Middleware And Staticfiles in Asp.Net Core 1.0 – Part 2


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 1 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
    {
        // 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!");
            });
        }
    }
}

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.

Asp.Net Core Folder Structure

Asp.Net Core 1.0 : Folder Structure

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

StaticFiles is Inside the folder

StaticFiles is Inside the folder

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

Outside the wwwroot

Outside the wwwroot

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") // listing directory details for specified folder.
            });

Output

Directory Browsing in ASP.NET Core 1,0

Directory Browsing in ASP.NET Core 1,0

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") // 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

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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: