Last post Aug 01, 2007 09:11 AM by aweil
Jul 31, 2007 10:29 AM|Nom84|LINK
So far they way we have been using web.config on all our servers is like mostly how everyone does it. But now we need environment based web.config. At the moment we have one web.config with different values for the same keys on different servers. For example,
we can have one connection string Key for the local webconfig which is pointing to the local database. When it goes to the server we have to change the connection string to point to the db on the server.
Now i require a way to have same web.config on all the servers where i just upload the web.config without the need to change the values it should just pick the values correctly depending on the server its on or the machine it is on. So it will be preloaded
with all the required values for every server and it should pick up the right values based on the environment its running in.
I know its confusing so is there an easier way of doing this?? or any way at all??
Any help will be appreciated.
Jul 31, 2007 04:10 PM|aweil|LINK
Sure, we all have the same problem and there's a simple solution: place in your Web.config only what's common to all servers. For the bits that vary (e.g. database connection string), simply put them in an external config file.
When you upload your application to a server, you'll upload everything including the Web.config, but you won't overwrite the external config file.
Practically, the way to do this is (borrowed from
<?xml version="1.0" encoding="utf-8" ?>
<add key="MyKey" value="MyValue" />
Hope this helps,
Aug 01, 2007 08:53 AM|Nom84|LINK
Thanks aweil. Thats one way . But again would it no be the same as having one webconfig. As even if you the external webconfig it would have to change when deployed to different servers. We have two servers we need to deploy on. One on Test and one on Production.
Now the connection strings and few other values are different in both of these servers as well as in our local server.
So if there is an external webconfig file with connection strings and values that change we will have to change those values of the external config file on those servers as well to point to their respective db's or ip addresses. I am looking for more like
a way where we can have different values for the same key and the application picks up the right values depending on the server it is on.
One other way is to get the machine name in the global asax and populate those values in a static class based on the machine name like specified in this article http://www.codeproject.com/aspnet/OneWebConfig.asp but that would mean change everwhere the code
is referencing directly the webconfig to reference the static class variables and also you can only have one remote machine values where as we require for 2.
The problem is that they dont want to change the webconfig on the server so when we do an upload it should have the values already for that server and when they move the whole solution to the other servers those values should not require any changes on their
Aug 01, 2007 09:11 AM|aweil|LINK
Not as complicated as you think...
Look, if you use an external config file (let's say Custom.config), it contains only the connection strings and "few other values". You set them once and for all on each server. Whenever you deploy your application again to those server, you copy everything
(including the Web.config) except Custom.config.
What do you gain over having a single Web.config? Well, suppose you add a new parameter in Web.config that's common to all servers: you can deploy the whole application overwriting the previous Web.configs. If you had only one file, you would have to modify
the Web.config to add that parameter on each server... Am I clear?
Now, there's a second way to go: place the connection string in the Machine.config file of each server (in the folder C:/Windows/Microsoft.NET/Framework/vXXXXX/CONFIG/). You can
now remove it from the Web.config and the application will automatically take it from Machine.config. So when you deploy your application, you can deploy everything including the Web.config to all servers. They'll look at both their Web.config and Machine.config
to get their final configuration.