views:

194

answers:

2

I've followed advice on how to setup 404s by:

http://www.andornot.com/about/developerblog/archive/2009%5F10%5F01%5Farchive.aspx

and related:

http://stackoverflow.com/questions/667053/best-way-to-implement-a-404-in-asp-net

From Global.asax:

protected void Application_Error(Object sender, EventArgs e)
{   
    Exception exception = Server.GetLastError();
    if (exception is HttpUnhandledException)
    {
        if (exception.InnerException == null)
        {
            Server.Transfer(string.Format("~/Error.aspx", false));
            return;
        }
        exception = exception.InnerException;
    }

    if (exception is HttpException)
    {
        if (((HttpException)exception).GetHttpCode() == 404)
        {
            Server.ClearError();
            Server.Transfer("~/404.aspx", false);
            return;
        }
    }

    if (Context != null && Context.IsCustomErrorEnabled)
    {
        Server.Transfer(string.Format("~/Error.aspx"), false);
    }

}

And from Web.config:

<customErrors mode="On"/>

It all works beautifully locally while testing (VS2010) but in production (ISS6) it only works for aspx pages. http://mysite.se/foo.js get me the ISS 404 page. ("The page cannot be found")

What am I missing?

+1  A: 

The 404 handler specified in the Web.Config only deals with files handled by the ASP.NET runtime, all others including JavaScript files will be handled by the 404 page specified in your IIS settings. This is the reason why you are seeing the IIS generated error message for http://mysite.se/foo.js instead of the one specified in the custom errors section of the Web.Config.

You can, however, map these file types to the aspnet_isapi.dll to have them handled by your custom error pages.

  1. Open IIS Manager
  2. Select your Web Site
  3. Right click, in the context menu select Properties
  4. In the Extension column find .aspx, double click and copy the full path to aspnet_isapi.dll. It should be something like C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\« aspnet_isapi.dll
  5. Click Add and paste the path in the Executable box
  6. In the Extension box type .html
  7. Make sure Check that file exists is NOT checked
  8. Dismiss all dialogs

See here for more information.

Phaedrus
+3  A: 

If you don't want to set up wildcard mappings, or have ASP.NET handle all your static files (and generally performance might well say you don't), you need to configure IIS 6 to send 404's to an aspx page that handles the errors.

  1. Open IIS Manager, browse to the site in question, right click on it, and select Properties.
  2. Open the "Custom Errors" tab.
  3. Scroll down to 404, select it, and press "Edit...".
  4. Change message type to URL and point it at a file that exists on your server (i.e "/404.aspx").

Point 4 is the key - it needs to point to a file that exists, otherwise IIS will revert to the default.

Zhaph - Ben Duguid