At my workplace we have many test environments to manage, and we generally run a .NET stack for websites. We currently use Octopus Deploy which, for each environment, will configure applications at deploy time. Our apps are built and packaged up with replaceable variables which Octopus fills in on deployment.
Even if you don't use such a tool I would recommend configuration at deployment time. Your application that you package up (in your CI tool of choice) for deployment should be the same repeatable package that goes into dev, test and prod. We use Team City which builds our deployment packages, and another tool which is a package repository to store all the build artefacts.
Having code paths that are special for an environment potentially makes testing harder since your application is behaving differently.