3

In my web.config file I have custom errors enabled:

<customErrors mode="On" defaultRedirect="~/Error">
    <error redirect="~/Error/NotFound" statusCode="404" />
</customErrors>

My NotFound action:

public ActionResult NotFound()
{
    Response.StatusCode = 404;  //no issues when this is not set
    return View();
}

The problem: This configuration works fine on a local server, but when I move it to a remote server custom 404 pages are not shown (IIS default 404 is displayed) unless the status code of NotFound action is set to 200.

Could someone explain what's going on?

Maksim Vi.
  • 9,107
  • 12
  • 59
  • 85

1 Answers1

8

You also want to disable IIS custom errors by setting TrySkipIisCustomErrors to true.

public ActionResult NotFound()
{
    Response.StatusCode = 404; 
    Response.TrySkipIisCustomErrors = true; <---
    return View();
}
Win
  • 61,100
  • 13
  • 102
  • 181
  • Great, thank you. Does it mean it was manually set on the IIS server somewhere to use its own custom errors over application's? – Maksim Vi. Apr 08 '15 at 19:55
  • In IIS 7 **Classic mode**, TrySkipIisCustomErrors is set to true by default value. However, **I do not recommend running in Classic mode** since you can fix it by simply using `TrySkipIisCustomErrors = true;` while running on new **Integrated mode**. – Win Apr 08 '15 at 20:50
  • Good to know, but my local IIS is set to use integrated pipeline mode though and custom 404 handling seems to work just fine (the example above works well on a few other servers too). – Maksim Vi. Apr 08 '15 at 21:00