Introduction
This article provides a walkthrough of self-hosting a Web API. Here we will see how to host a web API inside a console application.
Step 1: To create a new project, open Visual Studio 2013 in Admin mode then click on "File" -> "New" -> "Project..." then select "Console Application".
Step 2: Install "Microsoft.AspNet.WebApi.SelfHost" through the Packager Manager console as shown below. Click on "Tools" then select "Library Package Manager" --> "Package Manager Console" and type the following command:
Install-Package Microsoft.AspNet.WebApi.SelfHost
Step 3: Create a Model and Controller and add the following implementation as shown below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SelfHost1
{
public class book
{
public int Id { get; set; }
public string Name { get; set; }
public string Author { get; set; }
public decimal Rating { get; set; }
}
}
Now add a public class named BooksController. Derive this class from "System.Web.Http.ApiController".
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web.Http;
namespace SelfHost1
{
public class BooksController
{
book[] books = new book[]
{
new book { Id = 1, Name = "Sagar", Author = "seventh", Rating = 5 },
new book { Id = 2, Name = "Sachin", Author = "seventh", Rating = 4 },
new book { Id = 3, Name = "Dravid", Author = "seventh", Rating = 3 },
new book { Id = 4, Name = "Ramesh", Author = "seventh", Rating = 2 },
new book { Id = 5, Name = "Ganguly", Author = "seventh", Rating = 1 },
new book { Id = 6, Name = "Rahul", Author = "seventh", Rating = 6 },
};
public IEnumerable<book> GetAllBooks()
{
return books;
}
public book GetBookById(int id)
{
var book = books.FirstOrDefault((p) => p.Id == id);
if (book == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return book;
}
public IEnumerable<book> GetBooksByStandard(string author)
{
return books.Where(p => string.Equals(p.Author, author,
StringComparison.OrdinalIgnoreCase));
}
}
}
Let's host a Web API.
Open the Program class file from the Solution Explorer.
using System;
using System.Web.Http;
using System.Web.Http.SelfHost;
namespace SelfHost1
{
class Program
{
static void Main(string[] args)
{
var config = new HttpSelfHostConfiguration("http://localhost:8080");
//To determine which action to invoke, the framework uses a routing table. The Visual Studio project template for Web API creates a default route
config.Routes.MapHttpRoute(
"API Default", "api/{controller}/{id}",
new { id = RouteParameter.Optional });
using (HttpSelfHostServer server = new HttpSelfHostServer(config))
{
server.OpenAsync().Wait();
Console.WriteLine("Listening for HTTP requests.");
Console.WriteLine("(Please Run the ClientApp project to send requests).");
Console.WriteLine();
Console.WriteLine("You can hit Enter to quit.");
Console.ReadLine();
}
}
}
}
Run the application; the following window should appear:
Summary
In this article, I explained how to self-host a Web API. In the next part we will see how to call a Web API from a client application.