2

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);
    }
}
Joakim
  • 513
  • 1
  • 6
  • 10
  • To be able to get your web server and local to act the same, set the IIS errorMode setting to Custom instead of DetailedLocalOnly (which is the default). See this site about where it's set http://www.iis.net/configreference/system.webserver/httperrors – Mark Seefeldt Mar 18 '14 at 19:00

2 Answers2

6

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.

tpeczek
  • 23,867
  • 3
  • 74
  • 77
  • 2
    Solved it by configuring the Web server according to the response in [asp-net-web-api-httpresponseexception-400-bad-request-hijacked-by-iis](http://stackoverflow.com/questions/9985327/asp-net-web-api-httpresponseexception-400-bad-request-hijacked-by-iis) – Joakim Apr 17 '12 at 06:18
  • Joakim's apprach did not work for me. I needed to use the approach here: http://stackoverflow.com/a/25555786/788846 – bowerm Apr 29 '15 at 16:47
0

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());
Mark Seefeldt
  • 582
  • 4
  • 9
  • One thing I did notice is that I have to specify the second parameter (the content) or IIS would still display the custom error page instead. – Mark Seefeldt Mar 18 '14 at 20:56