uid | title | author | description | ms.author | ms.date | ms.assetid | msc.legacyurl | msc.type |
---|---|---|---|---|---|---|---|---|
web-api/overview/hosting-aspnet-web-api/use-owin-to-self-host-web-api |
Use OWIN to Self-Host ASP.NET Web API 2 | Microsoft Docs |
rick-anderson |
This tutorial shows how to host ASP.NET Web API in a console application, using OWIN to self-host the Web API framework. Open Web Interface for .NET (OWIN) d... |
riande |
07/09/2013 |
a90a04ce-9d07-43ad-8250-8a92fb2bd3d5 |
/web-api/overview/hosting-aspnet-web-api/use-owin-to-self-host-web-api |
authoredcontent |
This tutorial shows how to host ASP.NET Web API in a console application, using OWIN to self-host the Web API framework.
Open Web Interface for .NET (OWIN) defines an abstraction between .NET web servers and web applications. OWIN decouples the web application from the server, which makes OWIN ideal for self-hosting a web application in your own process, outside of IIS.
- Visual Studio 2013 (also works with Visual Studio 2012)
- Web API 2
Note
You can find the complete source code for this tutorial at aspnet.codeplex.com.
On the File menu, click New, then click Project. From Installed Templates, under Visual C#, click Windows and then click Console Application. Name the project "OwinSelfhostSample" and click OK.
From the Tools menu, click Library Package Manager, then click Package Manager Console. In the Package Manager Console window, enter the following command:
Install-Package Microsoft.AspNet.WebApi.OwinSelfHost
This will install the WebAPI OWIN selfhost package and all the required OWIN packages.
In Solution Explorer, right click the project and select Add / Class to add a new class. Name the class Startup
.
Replace all of the boilerplate code in this file with the following:
[!code-csharpMain]
Next, add a Web API controller class. In Solution Explorer, right click the project and select Add / Class to add a new class. Name the class ValuesController
.
Replace all of the boilerplate code in this file with the following:
[!code-csharpMain]
Replace all of the boilerplate code in the Program.cs file with the following:
[!code-csharpMain]
To run the application, press F5 in Visual Studio. The output should look like the following:
[!code-consoleMain]