Debug / release Web.Config

I know that web.config in Visual Studio 2010 provides the ability to switch from databases from debug mode to release mode.

Here is my Web.Release.config:

<?xml version="1.0"?> <!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 --> <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> <connectionStrings> <add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" /> <add name="Testing1" connectionString="Data Source=test;Initial Catalog=TestDatabase;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings> <system.web> <compilation xdt:Transform="RemoveAttributes(debug)" /> </system.web> </configuration> 

Here is my Web.Debug.config code:

 <?xml version="1.0"?> <!-- For more information on using web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=125889 --> <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> <connectionStrings> <add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" /> <add name="Live1" connectionString="Data Source=Live;Initial Catalog=LiveDatabase;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings> <system.web> <compilation xdt:Transform="RemoveAttributes(debug)" /> </system.web> </configuration> 

And this is my Web.config code:

 <?xml version="1.0"?> <!-- For more information on how to configure your ASP.NET application, please visit http://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> <system.web> <compilation debug="true" targetFramework="4.0" /> <authentication mode="Forms"> <forms loginUrl="~/Account/Login.aspx" timeout="2880" /> </authentication> <membership> <providers> <clear/> <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" /> </providers> </membership> <profile> <providers> <clear/> <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/> </providers> </profile> <roleManager enabled="false"> <providers> <clear/> <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" /> <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" /> </providers> </roleManager> </system.web> <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> </system.webServer> </configuration> 

When I publish my project, nothing is displayed in my Web.config file. Is my Live Database Connection String displayed?

+74
c # web-config
Apr 27 '11 at 22:18
source share
5 answers

The web.config conversions that are part of Visual Studio 2010 use XSLT to "convert" the current web.config file to its .Debug or .Release version.

In your .Debug / .Release files, you need to add the following parameter to the fields of the connection string:

 xdt:Transform="SetAttributes" xdt:Locator="Match(name)" 

This will cause each line of the connection string to find the corresponding name and update the attributes accordingly.

Note. You do not have to worry about updating the providerName parameter in the conversion files, since they do not change.

Here is an example from one of my applications. Here is the section of the web.config file:

 <connectionStrings> <add name="EAF" connectionString="Data Source=NTSQLT\S2K5TST;Initial Catalog=HR;User ID=EAFApp;Password=XXXX" providerName="System.Data.SqlClient" /> </connectionString> 

And here the web.config.release section does the correct conversion:

 <connectionStrings> <add name="EAF" connectionString="Data Source=NTSQLP\S2K5TST;Initial Catalog=HR;User ID=EAFApp;Password=YYYY" xdt:Transform="SetAttributes" xdt:Locator="Match(name)" /> </connectionStrings> 

One added note: Conversions occur only when the site is published, and not when it is simple with F5 or CTRL + F5. If you need to run the update with the given config locally, you will have to manually modify the Web.config file for this.

For more information, you can see the MSDN documentation.

https://msdn.microsoft.com/en-us/library/dd465326(VS.100).aspx

+124
Apr 27 '11 at 10:29
source share

This is possible using the ConfigTransform assembly ConfigTransform available as a Nuget package - https://www.nuget.org/packages/CodeAssassin.ConfigTransform/

All "web. * .Config" conversion files will be converted and output as a series of "web. *. Config.transformed" files to the assembly output directory, regardless of the selected assembly configuration.

The same goes for the app conversion files. *. Config in non-web projects.

and then add the following target to your *.csproj .

 <Target Name="TransformActiveConfiguration" Condition="Exists('$(ProjectDir)/Web.$(Configuration).config')" BeforeTargets="Compile" > <TransformXml Source="$(ProjectDir)/Web.Config" Transform="$(ProjectDir)/Web.$(Configuration).config" Destination="$(TargetDir)/Web.config" /> </Target> 

Posting the answer as this is the first Stackoverflow post to appear on Google on this topic.

+8
Sep 01 '14 at 2:26
source share

For the conversion to work in development (using F5 or CTRL + F5), I drop ctt.exe ( https://ctt.codeplex.com/ ) in the folder packages (packages \ ConfigTransform \ ctt.exe).

Then I register the pre-or post-build event in Visual Studio ...

 $(SolutionDir)packages\ConfigTransform\ctt.exe source:"$(ProjectDir)connectionStrings.config" transform:"$(ProjectDir)connectionStrings.$(ConfigurationName).config" destination:"$(ProjectDir)connectionStrings.config" $(SolutionDir)packages\ConfigTransform\ctt.exe source:"$(ProjectDir)web.config" transform:"$(ProjectDir)web.$(ConfigurationName).config" destination:"$(ProjectDir)web.config" 

For transformations, I use the SlowCheeta VS extension ( https://visualstudiogallery.msdn.microsoft.com/69023d00-a4f9-4a34-a6cd-7e854ba318b5 ).

+5
Jan 27 '16 at 7:16
source share

If you replace all the connection strings with news for the working environment, you can simply replace all the connection strings with production ones using this syntax:

 <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> <connectionStrings xdt:Transform="Replace"> <!-- production environment config ---> <add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" /> <add name="Testing1" connectionString="Data Source=test;Initial Catalog=TestDatabase;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings> .... 

The information for this answer is from this answer and this blog post .

notice . As others have already explained, this option will be applied only when the application publishes not during startup / debugging (by pressing F5).

+2
Feb 22 '17 at 18:01
source share

To make the conversion work in development (using F5 or CTRL + F5), try this extension:

https://github.com/Microsoft/slow-cheetah

0
Jun 25 '19 at 13:40
source share



All Articles