Serving Static Files Outside of wwwroot in Asp.NET Core 1.0

Serving Static Files Outside of wwwroot in Asp.NET Core 1.0

In ASP.NET Core 1.0 (which is out as RC2 at the time I’m writing this post), static content such as images, css files, javascript files, etc can be served up in the wwwroot folder once your web site contains the NuGet package for Microsoft.AspNetCore.StaticFiles.  But what if you wanted to serve up static files outside of wwwroot?  With a little work extending the application builder configuration, it is possible.

First, create a folder under the root (but outside of wwwroot) of the ASP.NET Core application.  In this example, I’ll name that folder ‘static’.  In this example, we’ll store static files within this folder.

Next, let’s create a static class (no pun intended) within the ASP.NET Core web application project to extend the ApplicationBuilder with a method that will allow for serving static content from this folder named ‘static’:

using System.IO;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting.Internal;
using Microsoft.Extensions.FileProviders;

public static class ApplicationBuilderExtensions
{
	public static IApplicationBuilder UseStaticResources(this IApplicationBuilder app, HostingEnvironment env)
	{
		var path = Path.Combine(env.ContentRootPath, "static"); // this allows for serving up contents in a folder named 'static'
		var provider = new PhysicalFileProvider(path);

		var options = new StaticFileOptions();
		options.RequestPath = ""; // an empty string will give the *appearance* of it being served up from the root
		//options.RequestPath = "/content"; // this will use the URL path named content, but could be any made-up name you want
		options.FileProvider = provider;

		app.UseStaticFiles(options);
		return app;
	}
}

Let’s walk through what this is doing.  On line 9 it is getting the physical path of the newly created ‘static’ folder.  Now that the app knows where these files physically reside, let’s take advantage of some optional URL routing.  Shown above on line 15, I’m setting the RequestPath to an empty string.  What that will do is re-route requests for static files in the root path of the app to the physical ‘static’ folder.  That means an HTTP request like this:

<img src='foo.img' />

will be routed to look for the foo.img file in the physical folder named ‘static’.

Alternately, if I wanted to use the ‘content’ folder name (or any other name) in HTML calls for these static resources, I could do so as shown in the commented-out line #16.  In that line, the URL would use the ‘content’ route, which doesn’t physically exist but in fact gets re-routed to the ‘content’ physical path.  In this case, the resources physically sitting in the ‘static’ folder would be accessed using that re-written path like this:

<img src='/content/foo.img' />

Finally, line 19 is needed to give your app the ability to actually serve up static files within your application (you’ll still need to have the NuGet package Microsoft.AspNetCore.StaticFiles in order to make this work).

But in order to make this all work, we need to actually use this ApplicationBuilder extension.  To do that, open up the Start.cs file of the ASP.NET Core web application.  In Start.cs there is a method named Configure that gets called at runtime and configures the HTTP request pipeline.  It is within here that the call to the UseStaticResources method is needed:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
	app.UseStaticResources((HostingEnvironment)env);
	
	//... the rest of your Configure method calls go here
}

The above line #4 calls the method you created which allows for the serving up of static files from whatever folder you designated outside of wwwroot.

Update 7/1/2016 – Microsoft released their official documentation for working with static files in ASP.NET Core; definitely worth a read.

Tags: , Filled Under: Programming Posted on: June 3, 2016

2 Comments on “Serving Static Files Outside of wwwroot in Asp.NET Core 1.0”

  • eddy

    June 27, 2016 at 9:32 pm

    nice post,

    what about i want my static folder to be selected base on the request in a multi tenant application?

    how should i proceed?

    thank you.

  • Hekkaryk

    September 21, 2016 at 3:45 am

    Another way to do it: https://docs.asp.net/en/latest/fundamentals/static-files.html#serving-static-files
    Or in code:
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
    app.UseStaticFiles();

    app.UseStaticFiles(new StaticFileOptions()
    {
    FileProvider = new PhysicalFileProvider(
    Path.Combine(Directory.GetCurrentDirectory(), @”StaticFilesFolderName”)),
    RequestPath = new PathString(“/URLStringToUse”)
    });
    }

    I’m actively looking for a way to access file outside wwwroot WITH USER AUTHENTICATION. If you have any idea how to do it, I would be very thankful for help 🙂

  • Leave a Reply

    Your email address will not be published. Required fields are marked *