2

I'm trying to add a 500 error handling page to my ASP.NET MVC3 project.

I want my custom error page displayed regardless of local or remote access. My website is running on IIS6,IIS7 & IIS7.5 Express

I want it displayed when:

  • An exception is thrown in Application_BeginRequest
  • An exception is thrown in Application_Error
  • An exception is thrown in a static constructor in the Website Project
  • An exception is thrown in a Controller
  • An exception is thrown in a view
  • An exception thrown anywhere pretty much.

I haven't been able to do in this, in fact I haven't been able to get any custom error pages to display at all.

My error page lives in ~/Views/Shared/Error.aspx

My Application_Error method in Global.asax.cs just logs the thrown exception.

My web.config has this:

<customErrors mode="On" defaultRedirect="~/Views/Shared/Error.aspx" redirectMode="ResponseRewrite">
</customErrors>
...
<system.webServer>
  <httpErrors errorMode="Custom" />
  ...
</system.webServer>

What am I missing? What do I need to do to handle these scenarios?

2 Answers 2

1

For IIS 7+, you're only missing the part that defines which httpErrors to handle with custom handlers:

<configuration>
   <system.webServer>
      <httpErrors errorMode="Custom">
         <remove statusCode="500" />
         <error statusCode="500" path="~/Views/Shared/Error.aspx" />
       </httpErrors>
   </system.webServer>
</configuration>

(The <remove /> tag is optional, depending on your web.config hierarchy.)

For IIS 6 and below, You have to set this via the IIS Manager by going to the appropriate Properties page, Custom Errors tab, then edit the appropriate HTTPError line to "Message type:" "URL" and "URL:" "~/Views/Shared/Error.aspx".

4
  • Sorry that doesn't seem to work. I am still getting the built-in IIS Error. The exception is being thrown from the Application_BeginRequest method in Global.asax.cs
    – icedtoast
    Aug 26, 2011 at 0:50
  • You may have other problems in the application or configuration that is keeping this part of the web.config from getting parsed. Are there any errors in your Event Viewer? Does this still not work either IIS 6 nor 7 after these changes?
    – Jon Adams
    Aug 26, 2011 at 0:54
  • The only error in the event viewer is that there is an unhandled exception being thrown in my Application_BeginRequest. I am using IIS 7.5 Express at the moment.
    – icedtoast
    Aug 26, 2011 at 3:26
  • Well then, I don't know what else to tell you, because for the question you asked, that is the correct configuration. Now it's general sysadmin stuff: Try on a different server/PC. Double-check that the configs you are editing are the configs you think you are editing. Re-register .Net with IIS. Etc.
    – Jon Adams
    Aug 26, 2011 at 15:22
0

The best approach is to find out why your BeginRequest is throwing an exception in the first place. This should not be happening. In Application_Error, one alternative is to use GetBaseException and then just redirect to your custom error page with the exception information.

1
  • I'm throwing the Exception on purpose, as the original error causing the throw in the BeginRequest has been fixed. (The bug was two parts: 1. The actual bug 2. The fact that the custom error page was not displaying )
    – icedtoast
    Aug 26, 2011 at 2:53

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.