0

I can't get ANY custom error pages to appear in IIS8.5!

When I run my MVC 5 app locally in Visual Studio, custom error pages are displayed. But when I deploy to IIS on my webserver, and navigate to the site sub-page with a 'bad URL' my custom 404 page should be displayed, but the default generic 404 page is shown instead.

Here is what I've done:

web.config

customError section has been deleted.

Added the following section

<system.webServer>
    <httpErrors errorMode="Custom" existingResponse="Replace">
      <remove statusCode="404" />
      <remove statusCode="500" />
      <error statusCode="404" responseMode="ExecuteURL" path="/Error/NotFound" />      
      <error statusCode="500" responseMode="ExecuteURL" path="/Error/ServerError" />      
    </httpErrors>
  </system.webServer>

Controller

namespace MyApp.Controllers
{
  [Authorize(Roles = "MyRole")]
  public class ErrorController : Controller
  {
    [AllowAnonymous]
    public ActionResult NotFound()
    {
        Response.StatusCode = 404;

        return View();
    }

    [AllowAnonymous]
    public ActionResult ServerError()
    {
        Response.StatusCode = 500;

        return View();
    }
  }
}

Views

Very simple and straight forwards (not included here).

IIS

Verified the IIS Error Page feature is identical to what's in my config. A page for each error status code. Feature and Pages are set to 'custom'.

Checked Windows Features\Web Server (IIS)\Web Server\Common HTTP Features\HTTP Errors is installed.

Developer Tools

Navigating to a sub-page spelt incorrectly shows I'm getting a 404 Client Error.

Help

I've tries many things! Am I missing something? IIS environmental issue, i.e. a setting somewhere?

Custom errors work in my IDE but not on IIS. I've spent over a day on this! Read many pages, articles, and similar questions on SO.

7
  • Question title states "working locally", what I meant was it's working in Visual Studio IDE, not browsing on the same IIS box. Sorry for any confusion. Commented Oct 31, 2015 at 14:09
  • What do you see when you manually navigate to one of your error actions, like /Error/NotFound? Commented Nov 1, 2015 at 5:28
  • @TiesonT. For Edge I get "HTTP 404 error That’s odd... Microsoft Edge can’t find this page" message. For FF & Chrome I just get blank pages. (Not sure why FF & Chrome don't show their generic browser 404 error pages.) Commented Nov 1, 2015 at 8:11
  • If you set a breakpoint in the action method for NotFound, is it reached? Commented Nov 1, 2015 at 14:14
  • @TiesonT.Yep goes into the NotFound() action method, Response.StatusCode is 200, and I set it to 404, and it all works perfectly in VS IDE, i.e. custom error page is being displayed. But not so in IIS, which is why I'm leaning towards an environmental issue on the webserver. Commented Nov 1, 2015 at 21:43

3 Answers 3

1

After several days of investigating, finally found the issue.

In IIS, my website was residing as an Application under the Default Web Site, e.g. Default Web Site\MyDevSite. (All my dev sites are under the default web site).

When I moved the website to it's own IIS site, custom error pages are now being displayed!

If anyone knowns the reason why having an Application in IIS prevents custom error pages from being displayed, please add a comment here. It would be good to understand why this occurs.

I hope this helps somebody out. Had me going for quite a while.

Sign up to request clarification or add additional context in comments.

Comments

0

How are you browsing IIS? If you're doing it from the same machine, then you aren't going to see your custom error pages, because the customErrors section will default to using RemoteOnly if not specified.

3 Comments

I'm browsing IIS remotely, i.e from my desktop --> internet --> IIS (site). I tried locally just to see what happens - same result as browsing remotely.
@ThomasVeil As long as they're different machines, that's fine. If you're effectively browsing localhost, then you'll get the behaviour you describe. That said, try adding <customErrors mode="RemoteOnly"></customErrors> to your IIS web.config. If that still doesn't work, try setting it to mode="On". If that doesn't work, then it might a problem elsewhere.
Thanks John, I tried <customErrors> with both RemoteOnly and On, and same thing, still not working. But a good idea.
0

the problem is with the path in web.config(path="/Error/NotFound"). the url path of NotFound page will be changed when you move your website under the Default Web Site. So, give the correct url of the page at path then it will work(this is worked for me).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.