Modifying web.config for development and production environments - visual-studio

I have a webApi project and after deployment to Dev environment I need to edit web.config and change connection to database from production configuration to development configuration and back if I need deployment to prod.
How can I automatically set webconfig for selected dev or prod in the release or debug I use in my project?

To specify the changes that you want to make in Web.config files, you use transform files. A transform file is associated with a build configuration.
If you want to create a transform file for a custom build configuration that does not exist, create the build configuration first by using Configuration Manager.
You can open Configuration Manager by selecting it from the Build menu.
In Solution Explorer, expand the application Web.config file.
If any transform files have already been created, the Web.config file is displayed in Solution Explorer with a symbol indicating that it can be expanded, and the transform files are shown when you expand the Web.config file.
The build configuration that a transform is for is indicated by a string in the file name. For example, a transform file for the Debug build configuration is named Web.Debug.config.
If no transform file exists for the build configuration that you want to specify settings for, in Solution Explorer, right-click the Web.config file and then click Add Config Transforms.
Open the transform file for the build configuration that you want to work with.
Edit the transform file to specify the changes that should be made to the deployed Web.config file when you deploy by using that build configuration.
The default transform file includes comments that show how to code some common transforms.
The following example shows how to use the Match locator and the SetAttributes transform attribute. The Match locator attribute identifies the add element in the connectionStrings section as the element to change. The SetAttributes transform attribute specifies that this element's connectionString attribute should be changed to "ReleaseSQLServer".
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings>
<add name="MyDB"
connectionString="ReleaseSQLServer"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
</configuration>
Save and close the transform file.
When you deploy the Web application by using the selected build configuration and by using either a deployment package or one-click publish, the Web.config file is transformed according to your specifications.

Related

app.config transformation and installation folder url - update location

Is there a way to change the installation folder url and the update location of an application depending on the current configuration, in the same way you can change app.config properties with app.config transformations?

Using transformed Web.config with IIS Express during debug

I have a Visual Studio application that has multiple Solution Configurations. There is a Web.config transform file for each configuration. For example, Web.Debug.config, Web.Release.config, etc.
We also have a couple of developers working on this project that have nonstandard SQL Express instance names due to the way they installed SQL Express and rather than having them continually editing Web.Debug.config to run in their environment I have setup a Solution Configuration for each of them and added the following to the very bottom of the .csproj file. This code does work in that it triggers the creation of Web.config and MyWebApp.dll.config in the VS /obj/Debug-DeveloperName/ folder.
The transformed .config files are perfect, but IIS Express still uses the root Web.config (not transformed).
Is there a way to get IIS Express to use these transformed Web.config files while debugging locally?
<UsingTask
TaskName="TransformXml"
AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v12.0\Web\Microsoft.Web.Publishing.Tasks.dll" />
<Target
Name="AfterCompile"
Condition="exists('Web.$(Configuration).config')">
<!-- Generate transformed config in intermediate directory -->
<TransformXml
Source="Web.config"
Destination="$(IntermediateOutputPath)$(TargetFileName).config"
Transform="Web.$(Configuration).config"
/>
</Target>
Using the web application's Web.Debug.Config works for most of us, but not all.
There must be a way of getting IIS Express to use the transformed Web.Debug-DeveloperName.config during local debug?
Does the transformed Web.config have to be copied into a different folder?
I faced this problem before and I found a solution. Unfortunately, the solution is not based on forcing IIS to use different name of the config, but if you follow steps below, you will just select the configuration and run you app (which is ewhat you need I think). The Web.config transform will occur before build and it will replace the original Web.config by the transformad one. Then, when the deployment (to local IIS Express) begins, it will already use the transformed one.
Here is step by step how I did this in one project (VS2012):
Right click on the project and select Unload
Right click on it again and select Edit
Go to the bottom of the file and append the follwing to the right over the "</Project>" tag (it will be last item)
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\WebApplications\Microsoft.WebApplication.targets" />
<Target Name="BeforeBuild" Condition="'$(PublishProfileName)' == '' And '$(WebPublishProfileFile)' == ''">
<TransformXml Source="Web.config" Transform="Web.$(Configuration).config" Destination="Web.config" />
</Target>
The condition there is to prevent duplicate transformation when publishing.
Save the file, right click on it and select Reload
Now, everytime you run a build, your Web.config will be transformed according to selected configuration.

Visual Studio 2013 Website Project - Change Configuration in Configuration Manager

I have Website Project in Visual Studio 2013. I have two Publishing Profiles, one for staging and the other for production. In Configuration Manager, I can create new Active Solution Configurations but cannot add new Configurations to my drop-down, Debug is the only option.
So now when I attempt to Publish using my production profile, the web.config is getting transformed by both the Web.Debug.Config and my web.Production.Config.
How do I add new Configurations to the dropdown?
How do I add new Configurations to the dropdown?
You can't. At least not for the purpose you need to use these new configurations for.
I'll make the assumption that you really need to know this:
How can I publish a web site without the debug flag being applied to the web.config by the debug transform?
This is already happening, just in a confusing way.
When you publish the site using the Publish Web Site wizard it applies the appropriate transforms to the web.config. In your case, Web.Debug.Config and Web.Production.Config.
And now for the confusing part:
By default, web.debug.config actually removes the debug attribute from the web.config.
<compilation xdt:Transform="RemoveAttributes(debug)" />
This happens for legacy reasons to do with the missing Web Site project file and the underlying build tools.
If you want to stop the debug transformation being applied and you have no custom configuration in it, you can delete web.debug.config. Just be sure to add
<compilation xdt:Transform="RemoveAttributes(debug)" />
inside the <system.web> element of each of your environment specific transforms (web.Production.Config, etc).
My recommendation is:
Leave web.debug.config untouched. In its default state.
Create a publish profile for each environment.
Add a transform for each publish profile (web.<configuration>.config)
Put any environment specific configuration into the appropriate transform file.
To get my publish settings working the way I wanted I had to:
Return web.debug.config back to it's default contents (I had some transforms in here that I was using for my staging environment)
For every publish profile in /App_Data/PublishProfiles/, right click and select Add Config Transform to create a new config file at the root of the site (note that it will not be nested under the default web.config unless the profile is titled debug or release, kinda odd if you ask me)
Add any relevant transforms to each of these new configs
Since all publish profiles in a website project seem to be stuck on a debug configuration, there was no way for me to avoid web.debug.config transforming my default web.config. With this solution, we simply don't use web.config for publish specific transforms.
I'd like to credit Chris O'Neill for his helpful, if verbose, answer that clued me into this solution.
You can modify/create the configuration of the project FSUResearch manually. The steps are:
In solution explorer:
Right click on the project -> Unload project -> Right click on FSUResearch (unavailable) -> Click on Edit FSUResearch.csproj
In this file you will see many 'PropertyGroup' tags. These PropertyGroups consists of different combinations of configuration and platform. So create a new PropertyGroup and give your own project configurations. Make sure to give the project configuration name that is already a solution configuration name (Understand build configurations).
For example:
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Production|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
Save the changes and then right click on the .csproj file again and reload the project.

Multiple build configuration in Advanced Installer

I am using Advanced Installer with Visual Studio 2010.
I managed to create an .aip project, but when I want to add the files from the relevant VS projects, I have to choose the exact location of these files.
I want to use more then one build configuration so I can use config transforms to change my .config files depend on the build configuration I choose.
This is a problem for me because when I compile in Debug the .exe & .dll files goes to bin\Debug, but when I compile in, lets say, Staging these files goes to bin\Staging.
How can I get Advanced Installer to get the right files, meaning get them from the target folder of the build configuration I chosen?
Advanced Installer does not support this by default, but with a little bit of tweaking you can get this working. Let me explain how:
the first requirement is to have your output folders generated by VS in the same parent folder, as you have them both placed in "bin\".
now you need to open your project in Advanced Installer GUI and do the following:
create two builds in Media page, called Debug and Staging
create a property called "Configuration" or what name would you like, from Install Parameters page
in the New Property dialog you will have options to set per-build values for your property. Set them to "Debug" and "Staging", i.e. the names of the folders created by VS
now go to File -> Options -> Path Variables and define a new path variable with your full path for the debug/staging, the one you current have in the project.
from the Home tab, in the toolbar, use the "Convert Paths" wizard and then save the project.
Now, it comes the tricky part, you will need to edit the project file in a text editor, like Notepad++, capable of saving the file in UTF-8 format. Once you open the file look for this XML node:
<COMPONENT cid="caphyon.advinst.msicomp.AppPathsComponent">
<ROW Name="BIN_DIR" Path="<your path>\bin\Debug" Type="2" Content="0"/>
You might have multiple variables here if you are already using this feature. You need to edit the value and replace "Debug" with "[|Configuration]".
Now you can save and build the project and it should pickup the correct files for each of the two builds.

configsource and installer

I have an project csproj, with app.config file, and logging Ent.Library section using configsource attribute. The logging section is in ahother file Config\loggingconfiguration.config.
I have a Installer Project for this csproj. But when I generate , the file loggingconfiguration.config is NOT include.
Any suggestions?
Did you check the properties of your file in the solution explorer? If you want to see it deployed the Build Action property should be "Content"

Resources