How to create a C# Console Application and configure Owin to host WebApi

Rocco Sen
Rocco Sen
Dec 23, 2017 · 2 min read

If you hate IIS, this article is for you. It will teach you how to use Owin to host WebApi in a console application. Ideally, you want OWIN to host your API in a Windows Service but that’s another article.

You can find the full project here:
https://github.com/RoccoSen/OwinToWebAPI

Screenshot of the solution

Prerequisites:

– Have Visual Studio 2017 Community Edition Installed

1. Create New Console App Project using the .NET Framework

File -> New -> Project

Create Console Application targeting the .NET Framework

2. Install OWIN using NuGet Package Manager Console

Install-Package Microsoft.AspNet.WebApi.OwinSelfHost

Install OWIN using Package Manager Console

3. Create the Startup.cs class for OWIN configuration. Create this in the root of your project.

using Microsoft.Owin;
using Owin;
using System.Web.Http;
[assembly: OwinStartup(typeof(OwinAPI.Startup))]namespace OwinAPI
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
// Configure Web API for self-host.
// Note: I prefer my routes to be "api/{controller}/{action}" instead of "api/{controller}/{id}"
HttpConfiguration config = new HttpConfiguration();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}",
defaults: new { id = RouteParameter.Optional }
);
app.UseWebApi(config);
}
}
}

4. Edit static void main to host OWIN.

Note: I’m using http://localhost:9000. Use whatever URL you would like OWIN to host.

using Microsoft.Owin.Hosting;
using System;
namespace OwinAPI
{
class Program
{
const string url = "http://localhost:9000";
static void Main(string[] args)
{
using (WebApp.Start<Startup>(url))
{
Console.WriteLine("Server started at:" + url);
Console.ReadLine();
}
}
}
}

5. Add an API controller.

I like to group my controllers together in a folder called “Controller”. Here is the code for the ApiController. It just returns an array of words.

using System.Collections.Generic;
using System.Web.Http;
namespace OwinAPI.Controller
{
public class TestController : ApiController
{
public IEnumerable<string> GetTest()
{
return new string[] { "One", "Two", "Three" };
}
}
}

6. Test (Make a request to URL see if everything works.)

Testing out the API

Noticed we are returning XML and not JSON? This is because XML is the default configuration. Ideally, you want this to be JSON. It can be done by updating the Startup class.

Rocco Sen

Written by

Rocco Sen

Keeping myself busy with fun side projects and learning about Artificial Intelligence.

More From Medium

Related reads

Also tagged Programming

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade