How to move web.config connectionStrings in a separate web.config file ?

In this post you will learn how to create a separate web.config file in ASP.NET or MVC to store development and production versions of connection strings.

Step 1. Create a PrivateConn.config file on root with below code

<connectionStrings>
  <add name="DataConn1" connectionString="Data Source=server;Initial Catalog=database1;Persist Security Info=True;User ID=sa;Password=xxxx;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
  <add name="DataConn2" connectionString="Data Source=server;Initial Catalog=database2;Persist Security Info=True;User ID=sa;Password=xxxx;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
</connectionStrings>

Step 2. Open main web.config file (on application root) and then define new config file path, as given below

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  ....other codes....
  <connectionStrings configSource="PrivateConn.config"/>
  ....other codes....
</configuration>

Step 3. That's it, run the application.

Hope this helps.

Comments

Popular posts from this blog