Joakim
Joakim

Reputation: 521

Deploying ASP.NET MVC 4 Web API

Problem deploying ASP.NET MVC 4 Web API

I try to deploy an ASP.NET MVC 4 Web API site. Everything work fine but if I call the server from the outside and return the HTTP status of 400 and upwards, the Web server takes over the response.

Call locally on the server work fine.

Example:

Work fine:
 http:// myTestServer.se/Test/api/Test/200 
 http:// myTestServer.se/Test/api/Test/399 

Does not work, the Web server takes over the response: 
 http:// myTestServer.se/Test/api/Test/400 
 http:// myTestServer.se/Test/api/Test/599 
      For those cases where there is an Error Pages is returned. For an invalid code is returned ”The custom error module does not recognize this error.”

if I make the calls locally on the server, it works fine:
 http://localhost/Test/api/Test/400
 http://localhost/Test/api/Test/599

My simple test code will return the received ID as HTTP status..

// GET /api/values/200
public HttpResponseMessage<string> Get(int id)
{
    try
    {
        HttpStatusCode StatusCode = (HttpStatusCode)id;
        return new HttpResponseMessage<string>("Status Code : " + StatusCode.ToString(), StatusCode);
    }
    catch {
        return new HttpResponseMessage<string>("Unable to convert the id", HttpStatusCode.OK);
    }
}

Upvotes: 2

Views: 5313

Answers (2)

Mark Seefeldt
Mark Seefeldt

Reputation: 592

Instead of returning a new HttpResponseMessage, use the Request so that the Response is fully hydrated and will make it back through IIS untouched.

return Request.CreateResponse(HttpStatusCode.OK, "Status Code : " + StatusCode.ToString());

Upvotes: 0

tpeczek
tpeczek

Reputation: 24125

This is so called "smart error messages" issue. The IIS is hijacking your error message and replacing it with his own. The typical work around is to set TrySkipIisCustomErrors to true:

Response.TrySkipIisCustomErrors = true;

I haven't checked if this would work with Web API. You can read more about the issue here and here.

Upvotes: 6

Related Questions