1

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
ProfK
  • 49,207
  • 121
  • 399
  • 775
  • 404 states that the service is not found, meaning that no one is listening on the address/port. Could be something in your test environment. If you are not using self-hosting, the service should be deployed to an IIS, in order for you to call the service. Here you should double check the port that it is running on. – Michael Dec 12 '16 at 16:47
  • Can you post a full http request and response including headers? – matcheek Dec 12 '16 at 18:38
  • @Michael I know what a 404 is, and I quote myself from the question, "Of [or] I call it from PostMan with a good url, **I get the expected result**...".I can **never** get the expected result when the API is not properly set up and listening on IIS. – ProfK Dec 12 '16 at 20:21
  • @matcheek I've posted the full successful request and response, with headers, This is when I use PostMan. I'm trying to figure out how to lof this raw data using `HttpClient` and will post those soonest. – ProfK Dec 13 '16 at 07:29
  • 1
    Try using for example `client.BaseAddress = new Uri("http://localhost/webapi/");` and then `HttpResponseMessage response = await client.GetAsync("api/values");` instead of `client.BaseAddress = new Uri("http://localhost/webapi");` and `HttpResponseMessage response = await client.GetAsync("/api/values");` – BNK Dec 13 '16 at 07:38
  • 2
    Please read https://stackoverflow.com/questions/23438416/why-is-httpclient-baseaddress-not-working – BNK Dec 13 '16 at 07:50
  • 1
    Thank you so much, @BNK. Make that an answer, so I can give you some points. Just include the link, and a summary of what the other answer says. – ProfK Dec 13 '16 at 14:37
  • When I come back in a year or so. Remember to add app.MapControllers(); – Patrick Cairns Nov 28 '22 at 01:22

1 Answers1

1

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

halfer
  • 19,824
  • 17
  • 99
  • 186
BNK
  • 23,994
  • 8
  • 77
  • 87