Zero friction parallel self hosting of ASP.NET Web API and static files using OWIN and Katana

Written on April 28, 2013

If you want to self-host your ASP.NET Web API and serve static files as well, a viable solution is to use OWIN and Katana.

Filip has been writing about both a while ago here. Since then, some moving parts have changed and there have been improvements in terms of usability.

One of the improvements is Visual Studio Tooling for Katana by which you get a Visual Studio 2012 Template.

Just get the Katana.0.21.0-pre.zip from the project website at CodePlex (*sigh* Winking smile) and unzip it.

Now, install the Katana.vsix and after that, run Visual Studio.

Create a new Project from Templates / Visual C# / Katana Console Application.

Hitting F5 will fire up a new Console Window showing "Started".

If you open http://localhost:12345 in your browser, you'll see this:

Katana / OWIN - take one

Now, let's add ASP.NET Web API to our host.

First, run this inside your NuGet Package Manager Console:

Install-Package Microsoft.AspNet.WebApi.Owin --Pre

This will install the ASP.NET Web API packages and the OWIN extensions for ASP.NET Web API.

After that, update the Startup.cs inside the Visual Studio Solution -- this is, how the file should look before the update:

Now, the updated one:

To get some ASP.NET Web API action, let's add a simple Controller:

Let's start our Application again by hitting F5. Pointing our Browser to http://localhost:12345/api/helloworld should result in this:

Katana / OWIN - Web API calling

As the title of this post also promised static files to be served, we need to do some further work:

Jump back to your NuGet Package Manager Console and run:

Install-Package Gate.Middleware

This will install the middleware required for static file handling using OWIN.

As with ASP.NET Web API, we have to register the static file extensions in our Startup.cs -- yet, another update:

Now let's add a default.html document to serve some HTML.

Make sure to change the "Copy to Output Directory" property of the default.html to "Copy if newer" or "Copy always".

Restart your application again and guess what may be the result...

Katana / OWIN - static files

Oh and by the way: if you don't like port 12345: take a look at the Program.cs Smile

That's it. Also make sure to check out the OWIN stuff at GitHub.