5

I want to show a custom error page when the project is in production (release mode) and show the default error page from asp.net when running the page in (debug mode).

I have three Web.config files in my project.

  • Web.config
  • Web.Debug.config
  • Web.Release.config

This is what I have tried but had no luck.

Web.config

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

Web.Debug.config

<customErrors mode="Off" xdt:Transform="Replace"></customErrors>

Web.Release.config

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

Does anyone know how I can do this?

Thanks in advance :)

Edit: After reading around, I'm thinking that this is a known bug but I wasn't able to find a bug fix for this. If anyone knows more about this, please share. Thanks

Farhan Ahmad
  • 5,148
  • 6
  • 40
  • 69
  • 1
    If your on vs2010 install the free slowcheeta extension. It gives each transformation xml file a right click -> Preview function that is very useful debugging these. – asawyer Aug 27 '12 at 13:07

2 Answers2

6

Okay. So I figured out what was wrong.

Web.config

<customErrors mode="Off" defaultRedirect="~/Error.htm"></customErrors>

Web.Debug.config

<compile debug="true" />
<customErrors mode="Off" xdt:Transform="Replace"></customErrors>

Web.Release.config

<customErrors mode="On" defaultRedirect="~/Error.htm" xdt:Transform="Replace">
     <error statusCode="404" redirect="~/Error.htm" />
</customErrors>

The changes above will keep error messages disabled by default and whenever the project is deployed on the production server, the settings in Web.Release.config will overwrite the default Web.config

Here is a list of resources / links that I found helpful:

I hope this helps anyone else that is trying to accomplish the same thing.

Community
  • 1
  • 1
Farhan Ahmad
  • 5,148
  • 6
  • 40
  • 69
1

This is a know bug. That feature can be used right now only as part of the deploy process. Please check this below post

How can I use Web.debug.config in the built-in visual studio debugger server?

or you could just use the 'default' web.config as your development/debugging version, and then the web.release.config would of course continue to be the release version, since its transforms are applied when you publish.

Hope it will help.

Community
  • 1
  • 1
Pankaj Agarwal
  • 11,191
  • 12
  • 43
  • 59