0

I am developing a project in C# MVC 5.

When developing I prefer to switch off exception handling e.g. in Global.asax.cs:

#if !DEBUG
    protected void Application_Error(object sender, EventArgs e)
    {
        Exception exception = Server.GetLastError();
        // save exception inf to DB
        Server.ClearError();
        Response.Redirect("/Home/Error");
    }
#endif

in my web.config I have two options:

<system.web>   
   <!--<customErrors mode="Off"></customErrors> -->
   <!-- or -->
   <customErrors mode="On">
     <error statusCode="404" redirect="~/Home/NotFound"/>
   </customErrors>
</system.web>

The problem is that I have to edit it manually and it would be nice if it was changed automatically when the program is running in debug/release mode.

I there a way to in my program to change in the code? I want something like:

MyFunction()
{
#if DEBUG
  SetCustomError("mode", "Off");
#else
 // Keep web.config content
#endif
}

And where in the MVC structure can I do it?

Anders Finn Jørgensen
  • 1,275
  • 1
  • 17
  • 33
  • *I have to edit it manually* Why you want to do like this? – Divyang Desai Oct 07 '16 at 11:30
  • I wouldn't do this that way. I would propose you to just log your exception into the database in `Application_Error` method w/o clearing error and redirect. Instead you could setup `web.config` transformation so that to see regular asp.net mvc diagnostic page with explanation of an error while you are debugging your application and see error page when you deploy your app to production. Check [this link](https://www.asp.net/mvc/overview/deployment/visual-studio-web-deployment/web-config-transformations) to get more information on configuration transforms. – Alexey Andrushkevich Oct 07 '16 at 11:32
  • 1
    Can't you use ``? – Andrei Olariu Oct 07 '16 at 11:34
  • @Andrei Olariu, you are right. Thank you. But I am curious if there are a way to change the settings in web.config, depending on you are in debug mode or not. – Anders Finn Jørgensen Oct 07 '16 at 11:42
  • 1
    You could have a look at this: http://stackoverflow.com/questions/11207323/change-custom-error-mode-in-web-config-programmatically-in-asp-net-mvc-3. I don't, however, think it's such a good idea. – Andrei Olariu Oct 07 '16 at 11:52
  • Possible duplicate of [When #if DEBUG runs](http://stackoverflow.com/questions/12960602/when-if-debug-runs) – TheVillageIdiot Oct 07 '16 at 11:56
  • Well, this is not a good idea. It is `preprocessor` constant. You can set it in build configuration of project. – TheVillageIdiot Oct 07 '16 at 11:58
  • It is bad idea to allow site's code to modify it configuration. Make sure you understand security implication of that – Divyang Desai Oct 07 '16 at 12:18

1 Answers1

0

As has pointed out Change Custom Error Mode in Web Config Programmatically in ASP.NET MVC 3 answers the question on how you can change the setting in your web.config in the code.

Some has argued that its a code smell to change your settings in the code. I agree but the point is that it should only be done when the code runs in debug mode. Not in production.

Community
  • 1
  • 1
Anders Finn Jørgensen
  • 1,275
  • 1
  • 17
  • 33