views:

354

answers:

4

Where can I setup custom errors for directories in my application such as App_Code, App_Browsers, etc.? I already have customErrors configured in the web.config and that works as expected. For example,

http://www.mysite.com/bla.aspx > redirects to 404 page

but

http://www.mysite.com/App_Code/ > displays "The system cannot find the file specified."

There's no physical App_Code directory for my site. Is this something that I can change in IIS?

A: 

One way is to provide a redirect in the global.asax file:

 void Application_Error(object sender, EventArgs e) 
{
    //uncomment this to narrow down 'helpful' microsoft messages
    //HttpRequest request = ((HttpApplication)sender).Context.Request; 


    Exception ex = Server.GetLastError();
    //ErrorManager is a custom error handling module
    ErrorManager.ProcessError(ex);
    Response.Redirect("~/error.aspx?error=" + HttpUtility.UrlEncode(ex.Message), true);
}

{ On a side note, I was getting an exception that I just couldn't track down - it just said 'file not found' but didn't say which file was missing. It turned out to be a broken image reference in a css file - breaking on line two of the code above helped identify the missing file }

flesh
I tried that but ended up with the same result. I've noticed several large sites running on .NET display the generic error. Maybe it's just me, but I'd prefer that ALL errors be handled gracefully to the user. Even SO displays a blank screen if you try to access the /App_Code directory.
Jonathan S.
+1  A: 

I believe you will need to set the error pages in IIS itself, as the requests you talk about never reach the ASP.NET application. The reason your first example works is because IIS recognises the .ASPX extension and forwards it to ASP.NET.

Robert Wagner
A: 

Add a Wildcard Mapping to IIS to run ALL Requests through ASP.net, then you can use Global.asax to handle the error.

Taken from here:

Follow these steps to create a wildcard script map with IIS 6.0:

  • Right-click a website and select Properties
  • Select the Home Directory tab
  • Click the Configuration button
  • Select the Mappings tab
  • Click the Insert button (see Figure 4)
  • Paste the path to the aspnet_isapi.dll into the Executable field (you can copy this path from the script map for .aspx files)
  • Uncheck the checkbox labeled Verify that file exists
  • Click the OK button
Michael Stum
+1  A: 

You are trying to server content from an Protected Folder... ??

I think you might need to allow access to these folders to get the nice errors you are looking for...

http://www.webdavsystem.com/server/documentation/hosting_iis_asp_net/protected_folders

That being said... there is a reason these folders are protected.

I would never put anything i needed IIS to serve in protected folders.

But there are always reasons to do do something? i have broke a few rules in my short lifespan :)

UPDATE:

Found this when i tried this locally: http://support.microsoft.com/kb/942047/

Looks like those reserved directories throw special 404's you might be able to get IIS to Target the 404.8 type... with out opening up serving to those directories

BigBlondeViking
No, I don't want to allow access. I just want the error message for these folders to have a consistent look as all other errors. Sorry if that was unclear.
Jonathan S.
No worries~, what is the likely-hood that a user who guesses that url, and they needs to see clean errors? As long as you never build a url that has those protected folders reference you wont ever be requested by a real end user, I would think the danger of allowing any type of serving from or base off those folder is not worth the gains of uniform error pages?Good Luck
BigBlondeViking
Yes I realize it's not a likely scenario. I was just wondering if there was an easy way to implement it. microsoft.com provides custom errors, while several other sites running on asp.net do not.
Jonathan S.
you also might want to submit this on Serverfault to see if a IIS admin has an Idea?
BigBlondeViking