Introduction

In this article, you will see how to find the XML output on the self-hosting URL. Here first we self-host the application on the URL "http://localhost:8080" and then copy this URL to the browser and add the "api/items" for showing the API controller value. Here we need the "Microsoft ASP. Net Web API Self Host" package.

Use the following procedure to create the sample application.

Step 1

First we create the console application.

Step 2

Set the ".Net Framework 4".

Step 3

We check that the Nuget Package Manager is installed or not.

For installation of "Nuget Package Manager":

Step 4

Now we install the Web API Self-Host Package.

Step 5

Create the Model class:

Add the following code:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Hosting
  7. {
  8. public class Item
  9. {
  10. public int Id { get; set; }
  11. public string Name { get; set; }
  12. public string Category { get; set; }
  13. }
  14. }

Step 6

To add a Controller class:

Add the Following Code:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Text;
  6. using System.Web.Http;
  7. namespace Hosting
  8. {
  9. public class ItemsController : ApiController
  10. {
  11. Item[] items = new Item[]
  12. {
  13. new Item { Id = 1, Name = "Apple", Category = "Fruit" },
  14. new Item{ Id = 2, Name = "Tomato", Category = "vasitable" },
  15. new Item{ Id = 3, Name = "T-Shirt", Category = "cloths" }
  16. };
  17. public IEnumerable<Item> GetAllItems()
  18. {
  19. return items;
  20. }
  21. public Item GetItemById(int id)
  22. {
  23. var item = items.FirstOrDefault((i) => i.Id == id);
  24. if (item == null)
  25. {
  26. throw new HttpResponseException(HttpStatusCode.NotFound);
  27. }
  28. return item;
  29. }
  30. public IEnumerable<Item> GetItemsByCategory(string category)
  31. {
  32. return items.Where(i => string.Equals(i.Category, category,
  33. StringComparison.OrdinalIgnoreCase));
  34. }
  35. }
  36. }

Step 7

Now we host our Web API.

Open the "Program.cs" file and add the following code into this file:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Web.Http;
  7. using System.Web.Http.SelfHost;
  8. namespace Hosting
  9. {
  10. class Program
  11. {
  12. static void Main(string[] args)
  13. {
  14. var config = new HttpSelfHostConfiguration("http://localhost:8080");
  15. config.Routes.MapHttpRoute(
  16. "API Default", "api/{controller}/{id}",
  17. new { id = RouteParameter.Optional });
  18. using (HttpSelfHostServer server = new HttpSelfHostServer(config))
  19. {
  20. server.OpenAsync().Wait();
  21. Console.WriteLine("Host The Application on Given URL");
  22. Console.ReadLine();
  23. }
  24. }
  25. }
  26. }

Step 8

Execute the application. It will show a Console output screen for displaying a message.

Confermation Message of Hosting

Now copy the URL and paste it into the browser and change it to "http://localhost:8080/api/items".

The output will be as:

XML Output