Home » Silverlight

webconfig substitute variable

i have a lot of connection Strings in webConfig. and i test application in different places

is in possible create variable in webconfig like @MyDataSource="my_pc\sql2008r2"

and use it in connection strings like

<add name="dbEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;

Data Source=@MyDataSource  instead of Data Source=my_pc\sql2008r2;

AttachDbFilename=|DataDirectory|\db.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />

after that i can change one line in wegconfig.


1 Answer Found


Answer 1

You can't do this directly, but there is a work around that works well.

You can modify the ConnectionStrings during the Application_Startup event in the Global.asax.  So the algorithm would look something like this:

- Add a MyDataSource key in the app settings

- In each of your connection  strings put a placeholder. Something like {MyDataSource}

- In the application  startup, spin thru the connection strings  (via ConfigurationManager.ConnectionStrings) and do string replaces with the value in the app settings.

That should get you done.  Now if you are using VS 2010, you can create  you multiple versions of the web.config (ie one for testing and one for production) to prevent you from having to go this route.

Hope this helps.





<< Previous      Next >>

Microsoft   |   Windows   |   Visual Studio   |   Sharepoint   |   Azure