Hello again, everyone! As I’m sure you already know, ASP.NET MVC 4 beta is available and it has some fantastic stuff in it! One of the interesting bits is the latest ASP.NET Web API.

Looking at that, I’m absolutely certain you’re thinking the same thing Glenn Block was saying on Twitter:

@gblock: hmm I really wish there was an Owin adapter for #aspnetwebapi! /cc:@loudej

Wish no longer! Gate has a new drop available, version 0.3.4, and it contains a Get.Adapters.AspNetWebApi package which does exactly that – enabling you to mix the Web API into your OWIN based web applications.

Let’s take a look at how that feels. That should be even easier than before because of a few more things that are new in Gate – a handful of Quickstart nuget packages, and a Ghost “generic host” process which lets you use any of the available OWIN http servers interchangeably. Neat, right? Let’s jump right into that!

First – start with a new “ASP.NET Empty Web Application”

Then – to make it “really empty” – let’s remove a bunch of references. This step is optional, but should be very satisfying if you want to see some minimalism in action. Quickest way is to select the last assembly reference and lean on the “delete” key.

Finally let’s add the meta-package Gate.Quickstart.AspNetWebApi from Nuget.org.

PM> Install-Package Gate.Quickstart.AspNetWebApi
Attempting to resolve dependency 'Gate.Quickstart.Core (≥ 0.3.4)'.
Attempting to resolve dependency 'Gate.Hosts.AspNet (≥ 0.3.4)'.
Attempting to resolve dependency 'Gate.Builder (≥ 0.3.4)'.
Attempting to resolve dependency 'Owin (≥ 0.7.0)'.
Attempting to resolve dependency 'Microsoft.Web.Infrastructure (≥ 1.0.0.0)'.
Attempting to resolve dependency 'Gate.Middleware (≥ 0.3.4)'.
Attempting to resolve dependency 'Gate (≥ 0.3.4)'.
Attempting to resolve dependency 'Gate.Adapters.AspNetWebApi (≥ 0.3.4)'.
Attempting to resolve dependency 'AspNetWebApi.Core (≥ 4.0.20126.16343)'.
Attempting to resolve dependency 'System.Net.Http.Formatting (≥ 4.0.20126.16343)'.
Attempting to resolve dependency 'System.Net.Http (≥ 2.0.20126.16343)'.
Attempting to resolve dependency 'System.Web.Http.Common (≥ 4.0.20126.16343)'.
Attempting to resolve dependency 'System.Json (≥ 4.0.20126.16343)'.

You can now press F5 and see this run. The quickstart adds an example Startup class -which is using a partial class trick because I wanted you to be able to add more then one demo to the same project. But that’s not really necessary – in your own projects a simpler Startup class like this would work exactly the same.

using System.Net;
using System.Net.Http;
using System.Web.Http;
using Gate.Adapters.AspNetWebApi;
using Owin;

namespace HelloEverything
{
    public class Startup
    {
        public void Configuration(IAppBuilder builder)
        {
            var config = new HttpConfiguration(new HttpRouteCollection("/"));

            config.Routes.MapHttpRoute(
                "Default",
                "{controller}",
                new {controller = "Main"});

            builder
                .RunHttpServer(config);
        }

        public void Debug(IAppBuilder builder)
        {
            builder.UseShowExceptions();
            Configuration(builder);
        }
    }

    public class MainController : ApiController
    {
        public HttpResponseMessage Get()
        {
            return new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent("Hello, AspNetWebApi!")
            };
        }
    }
}

Next you should try adding the meta-meta-package Gate.Quickstart.All. Then you’ll be able to press F5 to see a demo of everything and the kitchen sink: Web API, Nancy, SignalR, Gate “test page”, and Direct output from code (res.Write style).

Direct output is kind of interesting, looks like this:

    public class Startup
    {
        public void Configuration(IAppBuilder builder)
        {
            builder.RunDirect((req,res) =>
            {
                res.ContentType = "text/plain";
                res.Write("Hello, ").Write(req.PathBase).Write(req.Path).Write("!");
                res.End();
            });
        }
    }

Well – that’s probably long enough for this post. Next topic – hopefully soon – will be about using the Ghost.exe “generic host” to run this web application on HttpListener, Kayak, or Firefly.