Interfaces

Project

StaticFiles

NuGet packageOwin.Framework.StaticFiles
GitHub sourceOwinFramework.StaticFiles

Home |  Readme

Responds to requests by returning the contents of files in the file system. Supports directory mapping, security and file extension restrictions

This middleware serves static files. This middleware works with any output cache middleware that you have installed to ensure that files are cached both on the server and also by the browser.

Static files are files shipped with your website that contain content that can be directly consumed by the browser. These are usually HTML, CSS, JavaScript and image files, but can be any file that the browser can accept and process.

Adding this middleware to the Owin pipeline

 builder.Register(ninject.Get<OwinFramework.StaticFiles.StaticFilesMiddleware>())
    .As("Static files")
    .ConfigureWith(config, "/owinFramework/middleware/staticFiles");    
The assumes that you are using Ninject as your IoC container, and followed the getting started walkthrough. If this is not the case then you will need to adjust the code to work in your application.

Default Configuration

The configuration below is the configuration you will get by default if you do not provide a configuration for this middleware.

{
   "owinFramework": {
      "middleware": {
         "staticFiles": {
            "documentationRootUrl": "/owin/staticFiles/config",
            "staticFilesRootUrl": "/assets",
            "rootDirectory": "~\\assets",
            "enabled": true,
            "analyticsEnabled": true,
            "includeSubFolders": true,
            "maximumFileSizeToCache": 32768,
            "maximumCacheTime": "01:00:00",
            "requiredPermission": "",
            "fileExtensions": [
                { "extension": ".bmp", "mimeType": "image/bmp", "isText": false },
                { "extension": ".jpg", "mimeType": "image/jpeg", "isText": false },
                { "extension": ".png", "mimeType": "image/png", "isText": false },
                { "extension": ".gif", "mimeType": "image/gif", "isText": false },
                { "extension": ".html", "mimeType": "text/html", "isText": true },
                { "extension": ".htm", "mimeType": "text/html", "isText": true },
                { "extension": ".css", "mimeType": "text/css", "isText": true },
                { "extension": ".txt", "mimeType": "text/plain", "isText": true },
                { "extension": ".js", "mimeType": "application/javascript", "isText": true }
            ]
         }
      }
   }
}

Configuration Notes

  • You can restrict access to the static files by configuring the requiredPermission. For this to work you must add Authorization middleware to the Owin Pipeline.
  • The rootDirectory can be an absolute file path, UNC file path, relative to the current working directory, or start with ~ to use the MapPath method of IHostingEnvironment.This middleware will serve files in sub-directories of this root by default.
  • The staticFilesRootUrl is the URL within your website where the static files will be available for download.
  • With the Owin Framework the same middleware can be added to the Owin Pipeline multiple times with each instance having a different configuration. This is especially useful if you want to serve static files from different locations, or with different permissions.