Specify the active connection string to use in Web.Config - asp.net

Is there a quick and easy way to set a connection string in Web.Config to be the active connection string?
I basically want to name my connection strings appropriately and then set one as active without having to switch out the names or re-compile my application.
Something like this:
<add name="Current" connectionString="{Local}"/>
<add name="Local" connectionString=[...]" />
<add name="RemoteOnMyServer" connectionString=[...]" />
<add name="RemoteAzure" connectionString=[...]" />

I don't think that is possible the way you asked. But you can move the connection strings block to a separate file and then control which FILE is the active one:
<connectionStrings configSource="LocalDb.config"/>
Then you can have separate config files:
LocalDb.config
RemoteOnMyServer.config
RemoteAzure.config
<etc>
Each one of there would hold something like this:
<?xml version="1.0"?>
<connectionStrings>
<add name="namedConnectionString" connectionString="Data Source=..." providerName="..." />
</connectionStrings>
Swithcing between them then becomes a matter of changing the configSource on the <connectionStrings /> element.

Scott Hanselman has a good article describing using different configurations for different environments using the build setting in the compiler. I've used this with great success in some of my projects.
Have a look

Related

Using configSource attribute in Web.config to refer to another file, then using configSource attribute again

Our web.config has this section:
<connectionStrings configSource="connections.config" />
Our connections.config looks (something) like this:
<connectionStrings>
<add name="conn" connectionString="Data Source=source;Initial Catalog=db;Trusted_connection=True" />
</connectionStrings>
I think the reason we do this is to make it easier for our clients to change the database configuration. But this means that connections.config is checked into source control, and as a developer, I have to change it every time I am developing, and remember not to check those changes in.
As a result, I would like to use the configSource attribute again in connections.config, something like this:
<connectionStrings configSource="connections.local.config">
<add name="conn" connectionString="Data Source=source;Initial Catalog=db;Trusted_connection=True" />
</connectionStrings>
But when I do this, I get the error:
Parser Error Message: Unrecognized attribute 'configSource'. Note that
attribute names are case-sensitive.
Is it not possible to do this? It seems odd that the configSource attribute would only work in web.config itself.

Web.config connectionString transforms not working when using an external config file

Ok, I am using an external config file for my connection strings so each individual developer can have their own strings while developing. Normally, each dev has a different environment but we all publish to the same servers via web.release.config transforms.
However, when I publish from VS, it's not transforming from the web.release.config for the conn strings. I think it's because if you have the configSource attribute set to use an external config it ignores the transform.
Here's my web.config:
<connectionStrings configSource="userConn.config" />
And here's my userConn.config:
<?xml version="1.0"?>
<connectionStrings>
<add name="DefaultConnection"
providerName="System.Data.SqlClient"
connectionString="Data Source=XXXX;Initial Catalog=XXXX;user id=XXXX;password=XXXX;" />
<add name="ExtVariablesEntities"
providerName="System.Data.EntityClient" connectionString="metadata=res://*/Models.XXXX.csdl|res://*/Models.XXXX.ssdl|res://*/Models.ExtVariables.msl;provider=System.Data.SqlClient;provider connection string="data source=XXXX;initial catalog=XXXX;user id=XXXX;password=XXXX;MultipleActiveResultSets=True;App=EntityFramework;"" />
</connectionStrings>
After publishing, opening the actual web.config that made it to the server, it still has:
<connectionStrings configSource="userConn.config" />
Is there a workaround for this? I've had this setup before I just don't remember what the trick is.
Just an FYI that I was able to solve this by following this article: https://jshowers.com/simple-web-config-transforms-for-configuration-elements-that-use-configsource-and-external-files/
You end up with 3 custom config files - whatever you want to call them for dev, test and prod lets say. Then you will have 3 web config files dev, test and prod (these are your web config transform files) where you simply say:
<appSettings xdt:Transform="Replace" configSource="path.to.custom.config.file.depending.on.env">

How to keep private data separate in an open source ASP.NET MVC application

In an open source ASP.NET application I'm working on, I need to keep certain data in the configuration file private, while still keeping it easy for people to build and debug it on their own machine. This is data such as API keys, Mail Settings, etc..
How would I keep this data separate and out of the git repository while still allowing people to just pull and build without having to set up a bunch of stuff?
In your config file you can define configSource:
<configuration>
<appSettings configSource="filepath1.config" />
<connectionStrings configSource="filepath2.config" />
<!--etc-->
</configuration>
Put the configurations that you need to keep private in a separate config file, then exclude them in your .gitignore.
Keep in mind that this will ignore the whole section and overwrite it with the context you have in the referenced file.
You can also do Configuration Transform, which allows you to only overwrite a small set of variables in sections. For example:
In your main Web.config:
<configuration>
<appSettings>
<add key="Key1" value="Something I dont't Care"/>
<add key="Key2" value="Something dummy"/>
</appSettings>
</configuration>
And in your Web.Release.config:
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add key="Key2" value="Something I want to keep secret"
xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
</appSettings>
</configuration>
In this case the "Key2" value that you want to keep private will be in a separate file, and you can exclude the Web.Release.config through .gitignore.
Also there's another approach that I never tried, which can also overwrite config using external file.

Have connection strings as well as common config source in asp.net or wcf websites

Hi I am looking for a solution where I have some common connection strings which I have
a common connections strings file but also need some specific add and remove connection strings in some sites...but do not see a way to do it...
<configuration>
<connectionStrings configSource="connections.config">
<remove name="AppConnectionString" />
<add name="AppConnectionString" connectionString="data source=(local);initial catalog=xyz;integrated security=True;MultipleActiveResultSets=True;" />
<connectionStrings/>
</configuration>
With file connections.config containing
<connectionStrings>
<add name="name" connectionString="conn_string" providerName="System.Data.SqlClient" />
<add name="name2" connectionString="conn_string2" providerName="System.Data.SqlClient" />
</connectionStrings>
Thanks,
Bala
If this does not work as you have expressed it above (which is a shame in my opinon), you have a number of other options:
Include the common connection strings in the connection string config, and then use the application settings config for the app-specific connection strings.
As an alternative, use a custom configuration file for the app-specific connection strings and load it in for each app.
When you build your apps inject the correct connection string keys into the config file using something like powershell. This script can run as a post build task in your build script.
Otherwise, when you package or stage your release, ask the release team to run the script as a manual step (or add it into your deployment script).

Special characters in web.config

I've create a custom configuration section in my web.config file for my 3.5 web application. The custom configuration contains special characters listed below.
<add Key="2" String="â€"/>
<add Key="3" String="148"/>
<add Key="4" String="!X"/>
<add Key="5" String="¡§"/>
<add Key="6" String="¡¦"/>
<add Key="7" String="¡¨"/>
<add Key="8" String="’" />
<add Key="9" String="–" />
I currently have the xml type of the web.config defined as below.
<?xml version="1.0"?>
This works fine in one development environment, but when I migrate the application to another environment I get an xml parsing error on the string for Key 2. When I replace all the string definitions for each config entry with standard alpha chars, the application works fine. Is there a way to enforce the XML to be read the same way? I have looked into the encoding attribute for the xml definition tag, just not positive what to set it to. Any guidance would be well appreciated. Thanks in advance.
I recommend setting the encoding to "utf-8". Make sure the file is also actually saved as utf-8.
Are you typing these into your web.config file manually? Can't you just HTML-encode your string values?

Resources