WEB API 2 Self-hosted hostname issues
Asked Answered
L

1

7

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

Lashonda answered 4/6, 2014 at 8:58 Comment(0)
I
14

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/";

Impending answered 8/8, 2014 at 11:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.