Before reading this article, please go through the following articles:
-
Creating First HTTP Service using ASP.NET Web API: Part1 of Many
-
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.
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:
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:
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.
Accept the license term to install the package.
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:
The Controller class needs to be inherited from the ApiController class.
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:
Then create an instance of HttpSelfHostConfiguration; see:
Next add a default route; see:
After adding the default route we need to create an instance of HttpSelfHostServer and pass the configuration we just created as a parameter. See:
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.
Now browse to the URL and you should able to download the Bloggers detail as JSON.
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.