Search code examples
asp.net-mvcdebuggingconfigurationproduction

How to make code behave differently when running locally vs. in QA vs. production?


In an ASP.NET MVC application I have a controller set up to deliver e-mail notifications. When running locally on a development machine I want the e-mails delivered to the developer, when in QA I don't want any e-mail notifications going out and in Production I want the notifications going out to their intended recipients


Solution

  • Have three different web.configs and add an AppSetting which tells you where you are so you can determine if you should send an email.

    You could also define constants in your web.config using the CompilerOptions attribute:

    <system.codedom>
      <compilers>
        <compiler language="c#;cs;csharp" extension=".cs"
          type="Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0,
          Culture=neutral, PublicKeyToken=b77a5c561934e089"
          compilerOptions="/d:Test"/>
      </compilers>
    </system.codedom>
    

    And in your code use

    #if !Test
        SendMail();
    #endif