April 05, 2013

IIS overrides Response.StatusCode and showing default error page instead of custom message in ajax

in asp.net/mvc whenResponse.StatusCode is set, IIS 7.5 by default capture the response and inject default page according to the status code.

My following code was working fine on IIS 6, but as soon as i port this code on 7.5 i started getting following issue.

Response.StatusCode = 404; return new ContentResult { Content = "Not Found"}; 

ajax({type: "POST",url: d,data: f,success: function(h, j) {
g.redirectUrl();
},error: function(j, h) {
b._displayError(j.responseText);
}});

404 - FILE OR DIRECTORY NOT FOUND.

The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable.



It should have shown error message "Not Found" message.

Solution: To avoid IIS to inject default page. Add following into the Web.config of your app.

<system.webServer>
    <httpErrors existingResponse="PassThrough" />
  </system.webServer>

No comments: