How to Self-Host ASP.Net Web API: Part 3 of Many

Before reading this article, please go through the following articles:

  1. Creating First HTTP Service using ASP.NET Web API: Part1 of Many

  2. Consuming ASP.NET Web API Service using HttpClient: Part2 of Many 

This article is a step-by-step walkthrough of how to self-host an ASP.Net Web API. We are going to host a Web API in a console application. To do that create a Console Application.

Self-Host-ASP.Net-Web-API.jpg

After creating the Console Application make sure to change the target framework to .NET Framework 4.0. To change the framework right-click on the project and select Properties and choose .NET Framework 4 from the drop down; see:

choose-.NET-Framework4.jpg

Next we need to add a reference for the ASP.Net Web API. We will add a reference using NuGet. We will add the NuGet Web API package. To add this right-click on the project and click on Manage NuGet Package; see:

Manage-NuGet-Package.jpg

In the search box type Microsoft.AspNet.WebApi.SelfHost and click on the search button. Make sure to install Microsoft ASP.NET Web API Self Host in the project.

Microsoft.AspNet.WebApi.SelfHost.jpg

Accept the license term to install the package.

Accept-the-license.jpg

By this time we have set up the environment for self-hosting the Web API. Next we will add a Model class. To add that right-click and add a class to the project.|

Bloggers.cs

namespace webapihostapp

{

    public class Bloggers

    {

        public string Id { get; set; }

        public string Name { get; set; }

        public string AreaOfIntrest { get; set; }

    }

}

}

Next we will add a Controller class. To add that right-click and add a class to the project. Make sure that you are appending Controller with the class name. For example you want to give the controller name as abc then make sure that you are using the class name ABController. In this case we are using the controller name BloggersController; see:

BloggersController.jpg

The Controller class needs to be inherited from the ApiController class.

ApiController-class.jpg

Next we need to give an Action to the controller. We are writing a simple Action and this is returning a List of Bloggers. The Controller class will look like the following:

BloggersController.cs

using System.Collections.Generic;

using System.Web.Http;

 

namespace webapihostapp

{

   public class BloggersController :ApiController

    {

       public List<Bloggers> GetBloggers()

       {

           return new List<Bloggers>

            {

                new Bloggers { Id="1", AreaOfIntrest ="Sql Server " , Name ="Pinal Dave"},

                new Bloggers { Id="2", AreaOfIntrest ="ASP.Net " , Name =" Suprotim Agarwal " },

                new Bloggers { Id="3", AreaOfIntrest ="C Sharp " , Name ="ShivPrasad Koirala"},

                new Bloggers { Id="4", AreaOfIntrest ="Sql Server" , Name =" vinod Kumar " },

                new Bloggers { Id="5", AreaOfIntrest ="JavaScript " , Name ="John Papa"},

                new Bloggers { Id="6", AreaOfIntrest ="Dan Wahlin " , Name ="HTML5" },

                new Bloggers { Id="7", AreaOfIntrest ="Business Intelligence " , Name ="Stephen Forte"},

                new Bloggers { Id="8", AreaOfIntrest ="Web API " , Name ="Glen Block" },

                new Bloggers { Id="9", AreaOfIntrest ="Windows Azure " , Name ="Gaurav Mantri"},

                new Bloggers { Id="10", AreaOfIntrest ="Entity Framework" , Name ="Julie Lerman " },

                new Bloggers { Id="11", AreaOfIntrest ="HTML" , Name ="John Bristow"},

                new Bloggers { Id="12", AreaOfIntrest ="Silverlight" , Name ="Kunal" },

 

 

            };

 

       }

    }

}

As of now we have Model and Controller in place. We will write code in the Program file to host the Web API. The very first thing to do is add the following namespaces in Program.cs:

host-the-Web-API.jpg

Then create an instance of HttpSelfHostConfiguration; see:

HttpSelfHostConfiguration.jpg

Next add a default route; see:

add-a-default-route.jpg

After adding the default route we need to create an instance of HttpSelfHostServer and pass the configuration we just created as a parameter. See:

create-instance-of-HttpSelfHostServer.jpg

Consolidating all that together, the code to self-host the ASP.Net Web API would be as follows:

Program.cs
 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Web.Http;

using System.Web.Http.SelfHost;

 

namespace webapihostapp

{

    class Program

    {

        static void Main(string[] args)

        {

            var config = new HttpSelfHostConfiguration("http://localhost:9999");

            config.Routes.MapHttpRoute(

                "API Default", "api/{controller}");         

 

            using (HttpSelfHostServer server = new HttpSelfHostServer(config))

            {

                server.OpenAsync().Wait();

                Console.WriteLine("Press Enter to quit.");

                Console.ReadLine();

            }

        }

    }

}


Now press F5 to run the application.

Run-the-application.jpg

Now browse to the URL and you should able to download the Bloggers detail as JSON.

download-Bloggers-detail-as-JSON.jpg

In this way we can self-host an ASP.Net Web API in a console application. I hope you find this article useful. Thanks for reading.
 

Up Next
    Ebook Download
    View all
    Learn
    View all