Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WEB API 2 Self-hosted hostname issues

I'am trying to make a self-hosted web api service. I followed a tutorial and it works fine on my local computer.

localhost/api/values responds fine with the expected JSON.

Now, I have a server binded to the DNS "myserver.mycompany.com". When I start my WebApi 2 service on this server and try to call myserver.mycompany.com/api/values I have a 404 page not found error.

If I browse locally on this server and call the localhost/api/values url it works fine.

Here is the code of the Startup class :

using Owin;
using System.Web.Http;

namespace SelfHostedWebApi2
{
public class Startup
{
    // This code configures Web API. The Startup class is specified as a type
    // parameter in the WebApp.Start method.
    public void Configuration(IAppBuilder appBuilder)
    {
        // Configure Web API for self-host. 
        HttpConfiguration config = new HttpConfiguration();
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        appBuilder.UseWebApi(config);
    }
}
}

And here is how I start the server :

using Microsoft.Owin.Hosting;
using System;
using System.Net.Http;

namespace SelfHostedWebApi2 
{ 
public class Program 
{ 
    static void Main() 
    { 
        string baseAddress = "http://localhost:80/"; 

        // Start OWIN host 
        try
        {
            WebApp.Start<Startup>(new StartOptions(url: baseAddress));

            HttpClient client = new HttpClient();

            var response = client.GetAsync(baseAddress + "api/values").Result;

            Console.WriteLine(response);
            Console.WriteLine(response.Content.ReadAsStringAsync().Result); 

        }
        catch (Exception ee)
        {

            Console.WriteLine("Erreur : " + ee.ToString());
        }

        Console.ReadLine(); 
    } 
} 
} 

Thank you for your help

like image 811
Fred Mériot Avatar asked Jun 04 '14 08:06

Fred Mériot


1 Answers

You should change your baseAddress so that its endpoint matches your hostname or you may use a WeakWildcard * to match all possible hostnames.

This one should work: string baseAddress = "http://*:80/";

like image 98
ozanmut Avatar answered Sep 28 '22 20:09

ozanmut