Owin middleware for static files

 

Owin is a middleware that allow to define the application configurations so flexible and powerful and decouple server and application.

This layer is executed during the application startup and define the basic configurations of the application; for example, in a web application, it can define if the WebApi are used and the routes.

Another option is configure how the application will serve static files, who is the root and other options like if a folder will be browsable.

Static files

The first step is add to the root of the application the Startup.cs file, is not present;  then, some nuget packages are needed:

<package id="Microsoft.Owin" version="3.0.1" targetFramework="net461" />
<package id="Microsoft.Owin.FileSystems" version="3.0.1" targetFramework="net461" />
<package id="Microsoft.Owin.Hosting" version="2.0.2" targetFramework="net461" />
<package id="Microsoft.Owin.StaticFiles" version="3.0.1" targetFramework="net461" />

The next step is define the root folder of the static files, for example Static; now you can proceed with the Startup.cs file; the first step is define a PhysicalFileSystem object with the path fo the Static folder:

string root = AppDomain.CurrentDomain.BaseDirectory;
var physicalFileSystem = new PhysicalFileSystem(Path.Combine(root,"Static"));

Now you need to define some options for the Owin file server:

var fileServerOptions = new FileServerOptions
{
RequestPath = PathString.Empty,
EnableDefaultFiles = false,
FileSystem = physicalFileSystem,
EnableDirectoryBrowsing = true
};

In addition to the FileSystem property, we have defined if default files are enable for this folder (index.html, default.html an so on) and if the user can browse the directory.

We also have defined what is the request path; in this case, with the url http://hostname, the owin file server will serve the Static folder. The last step is enable the file server:

app.UseFileServer(fileServerOptions);

The Startup.cs file will look like this:

[assembly: OwinStartup(typeof(WebSiteOwin.Startup))]

namespace WebSiteOwin
{
 public partial class Startup
 {
 public void Configuration(IAppBuilder app)
 {
 string root = AppDomain.CurrentDomain.BaseDirectory;
 var physicalFileSystem = new PhysicalFileSystem(Path.Combine(root, "Static"));

 var fileServerOptions = new FileServerOptions
 {
 RequestPath = PathString.Empty,
 EnableDefaultFiles = true,
 FileSystem = physicalFileSystem,
 EnableDirectoryBrowsing = false
 };

 fileServerOptions.StaticFileOptions.ServeUnknownFileTypes = false;
 app.UseFileServer(fileServerOptions);
 }
 }
}

Directory browsing

Another option is using the owin directory browser to allow users to browse a specific directory.

For example, if we have in the application a folder called Browsable, we can add to the Owin middleware this configuration:

string root = AppDomain.CurrentDomain.BaseDirectory;
 var physicalFileSystem = new PhysicalFileSystem(Path.Combine(root, "Browsable"));

var directoryBrowserOptions = new DirectoryBrowserOptions()
 {
 RequestPath = new PathString(@"/Browsable"),
 FileSystem = physicalFileSystem
 };

app.UseDirectoryBrowser(directoryBrowserOptions);

 

Owin middleware for static files