I've created an ASP.NET web api on Windows that returns some test data as XML. I have installed the Nuget package Microsoft ASP.NET Web Api Self Host and in the web api I have configured a Selfhost with the port 1234. I have used the following documentation:
https://learn.microsoft.com/de-de/aspnet/web-api/overview/older-versions/self-host-a-web-api
I ran Visual Studio as an administrator and I also ran the web api. In the browser I entered localhost:1234/api/product/1. It works.
I'm trying to run this on Linux. The whole solution was copied to Linux and executed with MonoDevelop. When starting the web api and entering localhost:1234/api/product/1 in the browser, no data is returned. It loads infinitely.
Here is the code for the controller:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebServiceTest.Models;
namespace WebServiceTest.Controllers
{
public class ProductsController : ApiController
{
Product[] products = new Product[]
{
new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 },
new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M },
new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M }
};
[Route("api/product/getall")]
[HttpGet]
public IEnumerable<Product> GetAllProducts()
{
return products;
}
[Route("api/product/{id}")]
[HttpGet]
public IHttpActionResult Get(int id)
{
var product = products.FirstOrDefault((p) => p.Id == id);
if (product == null)
{
return NotFound();
}
return Ok(product);
}
}
}
Here is the code of the WebApiConfig class:
using System;
using System.Web.Http;
using System.Web.Http.SelfHost;
namespace WebServiceTest
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web-API-Konfiguration und -Dienste
var selfHostConfig = new HttpSelfHostConfiguration("http://localhost:1234");
// Web-API-Routen
selfHostConfig.MapHttpAttributeRoutes();
using (HttpSelfHostServer server = new HttpSelfHostServer(selfHostConfig))
{
server.OpenAsync().Wait();
Console.WriteLine("Press Enter to quit.");
Console.ReadLine();
}
}
}
}
How do I manage to run the web api on Linux?