3

I am using asp.net 4.5. I am trying to get an appsetting variable based on environment. How can I check what environment I am running?

    public virtual string GetConnectionString()
    {
        var connString = "";
        //if dev environment
          connString = ConfigurationManager.AppSettings["ConnStringTest"];
        //else if check for prod
         ...

        return connSring;
    }

Or is there a better way to do this?

Train
  • 3,420
  • 2
  • 29
  • 59
  • You could use [directives](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives/preprocessor-if) to check if you're in debug or release or set and check [environment variables](https://stackoverflow.com/questions/185208/how-do-i-get-and-set-environment-variables-in-c) or possibly [conditionals](https://stackoverflow.com/questions/4222338/when-to-use-preprocessor-directives-in-net). – Steven B. Nov 14 '17 at 23:49
  • 2
    [Web config transforms](https://learn.microsoft.com/en-us/aspnet/web-forms/overview/deployment/visual-studio-web-deployment/web-config-transformations) are common practice for updating connection strings based on environment during deployment (if that is what you are looking to do). – SpruceMoose Nov 14 '17 at 23:52
  • 1
    @CalC Actually I'm not. Thanks though. – Train Nov 14 '17 at 23:54
  • @lamelemon This won't work well if I have to enable Debug in production. Is there another way? – Train Nov 14 '17 at 23:58

0 Answers0