Search code examples
asp.netiisasp.net-web-apiiis-expressiis-10

Why does my Web API return 404 on all resources when called from a Console app?


I have a little Web API, almost directly from the standard VS project template, i.e. Home and Values controllers, and lots of MVC cruft. It is set to run and debug under IIS 10.

I have set up tracing by adding the package Microsoft.AspNet.WebApi.Tracing and the following code in WebApiConfig:

    public static void Register(HttpConfiguration config)
    {
        config.EnableSystemDiagnosticsTracing();
        SystemDiagnosticsTraceWriter traceWriter = config.EnableSystemDiagnosticsTracing();
        traceWriter.IsVerbose = true;
        traceWriter.MinimumLevel = TraceLevel.Debug;
        config.Services.Replace(typeof(ITraceWriter), new SimpleTracer());
        ...
        ...            
    }

SimpleTracer is an ITraceWriter that writes to a text file.

When I call the API from outside the VS ecosystem, i.e. from PostMan in Chrome, a bad url, that results in a 404 error message, and the creation of a new trace file if there's not already one. Of I call it from PostMan with a good url, I get the expected result, and a trace of the request in the trace file.

When I call it from my Console app, even with a good url, I still get a 404 error response, and nothing is written to the trace file. I made sure by removing it and IIS doesn't even re-create it when using the .exe client.

If I call it from the compiled .exe from outside VS, I get the same error.

Then, when I set the Web API to use IIS Express, everything works perfectly. Do I need CORS for calls from non-web apps, does IIS need an extra header in this case? What is wrong?

EDIT A: This is the request when I use PostMan, and it returns a 200 and the expected list of strings.

GET /DemoApi/api/values HTTP/1.1
Host: localhost
Content-Type: application/json
Cache-Control: no-cache
Postman-Token: f9454ffc-6a8d-e1ed-1a28-23ed8166e534

and the response and headers:

["value1","value2","value3","value4","value5","value6","value7"]

Cache-Control →no-cache
Content-Length →64
Content-Type →application/json; charset=utf-8
Date →Tue, 13 Dec 2016 06:20:07 GMT
Expires →-1
Pragma →no-cache
Server →Microsoft-IIS/10.0
X-AspNet-Version →4.0.30319
X-Powered-By →ASP.NET

EDIT B: This is the request sent using HttpClient:

GET http://abbeyofthelema/api/values HTTP/1.1
Accept: application/json
Host: abbeyofthelema
Connection: Keep-Alive

The only real difference is that because Fiddler doesn't capture traffic from localhost, I had to use my computer name instead. The same recipient still gets the request.

The response here is:

HTTP/1.1 404 Not Found
Cache-Control: private
Content-Type: text/html; charset=utf-8
Server: Microsoft-IIS/10.0
X-Powered-By: ASP.NET
Date: Tue, 13 Dec 2016 08:07:25 GMT
Content-Length: 4959

Solution

  • According to Timothy Shields at the following link

    Why is HttpClient BaseAddress not working?

    You must place a slash at the end of the BaseAddress, and you must not place a slash at the beginning of your relative URI