46

I want to set the custome error to be true to prevent users from viewing detailed info about my application. But I cannot find where I should write this <customErrors mode="on">; should it be in the web.config or in the web, debug.config or else where?

BR

zgpmax
  • 2,777
  • 15
  • 22
John John
  • 1
  • 72
  • 238
  • 501

3 Answers3

94

From my experience, we should turn custom error to On in release mode and turn it off in debug. To automatically do this, we can use web.config transformation like the following example.

Web.Debug.config

This setting will allow web server to display ASP.NET yellow page that contain useful error information.

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

Web.Release.config

In the other hand, we don't want user to know technical error. We should use custom error page instead of ASP.NET yellow page.

<customErrors mode="On" xdt:Transform="Replace" />
user229044
  • 232,980
  • 40
  • 330
  • 338
7

This will depend, but normally should be in the Web.config file.

The Web.Debug.config and Web.Release.config (and other configuration variations) are used for when you deploy your application. When you perform a publish operation, the transform is applied to your Web.config file during deployment, which means you can have specific configuration settings applied for debug, release, and other configurations that you have set up.

If you don't normally perform a publish operation during development, then you will need to apply this setting to the Web.config file in order for it to take affect.

See http://msdn.microsoft.com/en-us/library/dd465318.aspx for more details about transforming the Web.config file.

See http://msdn.microsoft.com/en-us/library/h0hfz6fc.aspx for an example of adding the customErrors element to the Web.config file.

devdigital
  • 34,151
  • 9
  • 98
  • 120
  • Scott Hanselman has a great video on Channel 9 about deployment and how to effectively use the debug and release versions of the web.config. http://channel9.msdn.com/Events/MIX/MIX10/FT14 – The Muffin Man Feb 15 '12 at 00:29
  • i define in the web.config before closing the tag as follow:- but if an error occur then a full error message (not custom error)will be displayed...!!! – John John Feb 15 '12 at 01:20
  • 2
    Place the element under the tag. You would also need to specify the defaultRedirect, either to a controller action, or preferably a static html file. Alternatively, many people have kept custom errors off, and then handled the routing in the application error handler. See http://stackoverflow.com/questions/6733064/how-do-i-display-custom-error-pages-in-asp-net-mvc-3 for details. – devdigital Feb 15 '12 at 09:31
  • thanks for ur reply; i insert the under the as in ; . but still an detailed error will be displayed. – John John Feb 15 '12 at 14:21
  • Have you set the defaultRedirect attribute to point to an action or a static html file? See the link I put in the comment for details. – devdigital Feb 15 '12 at 15:13
5

Put in in Web.config and create an error page for redirect. In MVC, you have HandleErrorAttribute, mark it on class to handler unexpected error, logged it and throw error page. Custom Error is default page for specific error with known status code.

<system.web>
    <customErrors mode="On">
      <error statusCode="404" redirect="/PageNotFound?" />
    </customErrors>
</system.web>
toannm
  • 485
  • 4
  • 9