I have an entry in my Web.Config file that indicates which environment I am in for connection strings and junk:
<add key="AppEnv" value ="2" /> <!--(0 = Dev, 1 = test, 2 = prod)-->
I am looking for a way to alert the developer, at the time of publishing, to make sure they have checked this key/value so that they don’t publish the ‘test’ to the ‘prod’ server and vice versa.
Thanks
I have come up with my own (probably unconventional) solution to this issue. We develop many different web projects for many different clients and have migrated all of them to this method due to all of the issues we have had with multiple web.config files, or required edits before publishing.
Basically, we let our app tell us which environment its running under based on the incoming URL. We initialize this on the first request and store it in memory for the life of the app. This way we can store each of our environment specific config values in the same config file and just qualify them with Development, Staging, Production, etc. And any settings that dont differ between environments dont need to be qualified.
First a sample web.config:
Next we have an “App” class that gives us access to our “Site” class, but you could architect your classes however you see fit.
We put our classes in our Biz Object class library. We just decided that it is not necessary to determine the environment on every single request, since it really cant change during the app’s lifetime. Also, this allows us to reference App.Site.Environment from ANYWHERE in code in the library or code behind. This is also helpful if you need to sprinkle some conditional logic in your code – like not sending emails to real people when running in dev/staging.
One last thing – for our Linq2SQL or EF Data/ObjectContexts, we do not store the connection string in the file, and instead overload the constructor so we can supply our correct environment connection string like this: