In this post, learn about self-hosting and how to Self Hosting Web API the ASP.NET Web API. You won’t need the IIS server for this topic. We use a console program to host the Web API.
Step 1
We start by developing the console application.
- The launch of Visual Studio 2012.
- “New Project” can be chosen from the Start page.
- Choose Installed template->Visual C# from the Templates menu.
- Change the name to “Hosting” and choose “Console application.”
- On the “OK” button, click.

Step 2
“.Net Framework 4” should be set.
- Right-click the project in the “Solution Explorer.”
- “Properties” should be chosen.
- Choose “.Net Framework 4” from the Destination Folder Drop Down List in the Properties dialogue.

- Open a popup to apply the adjustment, then select “Yes.”
Step 3
We determine whether or not the NuGet Package Manager is installed.
- Go to the Tools menu.
- The “Nuget Package Manager” is installed if the “Library Package Manager” is visible.
- It is not installed if “Library Package Manager” is not visible.
For “Nuget Package Manager” installation:
- Choose “Extension and Updates” from the “Tools” menu.
- Choose “Online” in the “Extension and Updates” dialogue box.
- The search term “nuget package management” should be entered.
- Choose “Install Nuget Package Manager” to do so.
Step 4
The Web API Self-Host Package is now installed.
- Choose “Library Package Manager” -> “Manages Nuget Packages For Solution” from the “Tools” menu.
- Type “Microsoft.AspNet.WebApi.SelfHost” in the “Nuget Package Manager” dialogue box.

- Click Install after selecting the ASP.NET Web API Standalone Host.
- shost14.jpg
- Choose “I Accept” from the menu.
Step 5
Establish the Model class:
- “Solution Explorer” is where.
- Choose “Hosting” -> “Add” -> “Class” from the context menu when you right-click the project.
- Rename the class to “Item” instead.
- On the “OK” button, click.
Put the following code in the “Item” Class:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Hosting
- {
- public class Item
- {
- public int Id { get; set; }
- public string Name { get; set; }
- public string Category { get; set; }
Step 6
Making a Controller class addition:
- “Solution Explorer” is where.
- Choose “Hosting” -> “Add” -> “Class” from the context menu when you right-click the project.
- Call the class “ItemsController” instead.
- On the “OK” button, click.
- In the “ItemsController” class, add the following code:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Web.Http;
- namespace Hosting
- {
- public class Items Controller : ApiController
- {
- Item[] items = new Item[]
- {
- new Item { Id = 1, Name = “Apple”, Category = “Fruit” },
- new Item{ Id = 2, Name = “Tomato”, Category = “vasitable” },
- new Item{ Id = 3, Name = “T-Shirt”, Category = “cloths” }
- };
- public IEnumerable<Item> GetAllItems()
- {
- return items;
- }
- public Item GetItemById(int id)
- {
- var item = items.FirstOrDefault((i) => i.Id == id);
- if (item == null)
- {
- throw new HttpResponseException(HttpStatusCode.NotFound);
- }
- return item;
- }
- public IEnumerable<Item> GetItemsByCategory(string category)
- {
- return items.Where(i => string. Equals(i.Category, category,
- StringComparison.OrdinalIgnoreCase));
- }
- }
- }
Step 7
We now Self Hosting Web API.
- Open “Program.cs” and enter the following code there:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Web.Http;
- using System.Web.Http.SelfHost;
- namespace Hosting
- {
- class Program
- {
- static void Main(string[] args)
- {
- var config = new HttpSelfHostConfiguration(“http://localhost:8080”);
- config.Routes.MapHttpRoute(
- “API Default”, “api/{controller}/{id}”,
- new { id = RouteParameter.Optional });
- using (HttpSelfHostServer server = new HttpSelfHostServer(config))
- {
- server.OpenAsync().Wait();
- Console.WriteLine(“Press Enter to quit.”);
- Console.ReadLine();
- }
- }
- }
Step 8
Use the following to add a new project to this application:
- “Solution Explorer” is where.
- To start a new project, right-click the project and choose “Add.”
- Make the name “Client” instead.

Step 9
With the following, install the “Microsoft ASP.NET Web API Client Library”:
- Choose “Extension and Updates” from the “Tools” menu.
- Choose “Online” in the “Extension and Updates” dialogue box.
- Enter “Microsoft.AspNet.WebApi.Client” in the search box.
- Installing it involves choosing the “Microsoft ASP.NET Web API library.”

Step 10
Use the following to add the client’s reference to the “Hosting” project:
- Right-click the “Client” project in the “Solution Explorer.”
- The “Add reference” option.
- Launch the “Reference” dialogue window.
- “Projects” is the answer you should select.

So, choose “Ok” from the menu.
Step 11
- The “Client” project’s “Program.cs” file should be opened.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Net.Http;
- namespace Client
- {
- class Program
- {
- static HttpClient client = new HttpClient();
- static void Main(string[] args)
- {
- client.BaseAddress = new Uri(“http://localhost:8080”);
- ListAllItems();
- ListItem(1);
- ListItems(“fruit”);
- Console.WriteLine(“Press Enter to quit.”);
- Console.ReadLine();
- }
- static void ListAllItems()
- {
- HttpResponseMessage resp = client.GetAsync(“api/items”).Result;
- //resp.EnsureSuccessStatusCode();
- var items = resp.Content.ReadAsAsync<IEnumerable<Hosting.Item>>().Result;
- foreach (var i in items)
- {
- Console.WriteLine(“{0} {1} {2}”, i.Id, i.Name, i.Category);
- }
- }
- static void ListItem(int id)
- {
- var resp = client.GetAsync(string.Format(“api/products/{0}”, id)).Result;
- //resp.EnsureSuccessStatusCode();
- var item = resp.Content.ReadAsAsync<Hosting.Item>().Result;
- Console.WriteLine(“ID {0}: {1}”, id, item.Name);
- }
- static void ListItems(string category)
- {
- Console.WriteLine(“items in ‘{0}’:”, category);
- string query = string. Format(“api/items?category={0}”, category);
- var resp = client.GetAsync(query).Result;
- resp.EnsureSuccessStatusCode();
- var items = resp.Content.ReadAsAsync<IEnumerable<Hosting.Item>>().Result;
- foreach (var item in items)
- {
- Console.WriteLine(item.Name);
- }
- }
- }
- }
Step 12
Then Run the application now.
- Right-click the “Hosting” Project in the “Solution Explorer.”
- choose “Set as Start Up project” next.

- Press F5 for execution.

Step 13
Use the following to debug the hosting project:
- Right-click the project “Client” in the Solution Explorer.a
- “Debug” -> “Start new instance” should be selected.

Output
