Skip to content

ASP.NET Self Host Static File Server

Sponsor: Do you build complex software systems? See how NServiceBus makes it easier to design, build, and manage software systems that use message queues to achieve loose coupling. Get started for free.

Learn more about Software Architecture & Design.
Join thousands of developers getting weekly updates to increase your understanding of software architecture and design concepts.


Continuing in my Owin and Katana series of blog posts, I’m going to demo an ASP.NET Self Host Static File Server. If you have not seen my previous posts in this series, I’ve covered how to self host a Web Api application and how to use Topshelf to debug and run as a windows service. The wonderful thing about the shift to Owin and Katana with ASP.NET is middleware. Katana has a file server implementation that you can plugin to your application startup pipeline in order to serve static files either on a filesystem or from embedded resources.

Middleware

The first thing we will do with our demo application is install the Owin StaticFiles middlware into our project.
Install-Package Microsoft.Owin.StaticFiles
The simplest possible configuration is to call IAppBuilder.UseFileServer(true).  This will enable directory browsing of files from the current location of the executable.
When running our demo application, this results in showing us the directory listing when we pass thru our Web API routes without a match. staticfiles1 Now this isn’t very useful but it displays the simplicity of different Owin middlewares that are available. UseFileServer has several different overloads:
Name Description
System_CAPS_pubmethodSystem_CAPS_static UseFileServer(IAppBuilder)
Enable all static file middleware (except directory browsing) for the current request path in the current directory.
System_CAPS_pubmethodSystem_CAPS_static UseFileServer(IAppBuilder, Boolean)
Enable all static file middleware on for the current request path in the current directory.
System_CAPS_pubmethodSystem_CAPS_static UseFileServer(IAppBuilder, FileServerOptions)
Enable all static file middleware with the given options
System_CAPS_pubmethodSystem_CAPS_static UseFileServer(IAppBuilder, String)
Enables all static file middleware (except directory browsing) for the given request path from the directory of the same name
For our demo application, we are going to create our File Server to serve files from an Assets directory that lives in the same location as our executable.
Now when we browse we see the files within the Assets directory. staticfiles2

Content Type Provider

The Katana FileServer only serves known content types.  If you try and browse to a file that is not a known content type, it will return a 404. In my example above, I’ve added a data.json file.  Odd to me was that a json file is not a known content type.  In order to serve json files, we must create our own content type provider and add our mappings.
Now we need to specify our CustomContentProvider in our FileServerOptions.

Source Code

GithubThe source code for this demo is available on Github.

8 thoughts on “ASP.NET Self Host Static File Server”

  1. Pingback: The Morning Brew - Chris Alcock » The Morning Brew #1925

  2. Pingback: Les liens de la semaine – Édition #145 | French Coding

  3. Pingback: #ASP.NET Weekly – the Top ASP.NET Content of the Week 20150817 @ Scott Ge

  4. Pingback: How-To Create Katana Middleware - CodeOpinion

  5. Pingback: ASP.NET Weekly 20150817 – the Top ASP.NET Content of the Week | Scott Ge

  6. Great article, well described and great layout.

    One thing I discovered is that the config.Routes.MapHttpRoute (gistfile1.cs Lines 7-11) is not required because it will never use the path /api/{controller}/{id} that path when serving files statically.

    I found this misleading because I tried to use the path in that route with no success, until I noticed that the url used in your example had no path.

Leave a Reply

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