ASP.NET c#: different web.config for different domains

Hi all

I have a website which communicates with an SQL database. In the websites web.config file, I create a "ConnectionString" to tell the program where the database is located. The website is made in Visual Studio 2010 and it is running Framework 4.

At the moment I run the website and database on a test server from a test domain. I have now moved the website to another server and database, and run the website from another domain.

Is it possible to create two web.config files, so that when I open the website on my test domain, it will connect to the database on the test server, and when I connect to the website on the "active" domain, it will connect to the database on the "active" server?

Both servers and domains are up and running, all I need is a way to connect to the right database depending on which domain I open the site from.

If any more information is needed, don't hesitate to ask.

Søren Augustesen
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


you can make the connection string in an external file, so whenever you update the web.config file, you will not have to fix the connection string


create file "Connection.config"

<?xml version="1.0"?>
    <add name="ConnectionString" connectionString="Data Source=;Initial Catalog=datatbase;User ID=username; Password=password;Connect Timeout=0" providerName="System.Data.SqlClient"/>

in web.config change connection string tag to this

 <connectionStrings configSource="Connection.config"/>

This should fix your problem

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Manoj PatilSr. Software EngineerCommented:
If you are the administrator of the website then you can create a sub-domain in your Domain.
If you create a sub-domain then you can execute your both sites with different Web.config

When you create sub-domain then it will create a folder of the sub-domain name in root folder.

you can upload complete website with web.config in that folder

the browser path will be like :
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.