Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I do Web.config Transformations with Visual Studio Website Projects?

It doesn't seem to be possible to change the Build Configuration of Visual Studio 2010 Website Projects (as opposed to Visual Studio Web Applications), and changing the Build Configuration is a key part of enabling Web.config transformations (it's not possible to change the configuration to anything except Debug).

How do I get Web.config transformations to work with Visual Studio 2010 Website projects if it's not possible to change the Build Configuration?

like image 850
Ryan Shripat Avatar asked Feb 22 '11 18:02

Ryan Shripat


People also ask

Where is the web config file in Visual Studio?

config file is located in the %SystemRoot%\Microsoft.NET\Framework\%VersionNumber%\CONFIG\ folder. The default settings that are contained in the Machine.


1 Answers

I'd prefer not to use entire an Web Application Project solution out of box. My solution is to use the XmlTransform task defined in Microsoft.Web.Publishing.Tasks.dll directly (this task is the core of WebConfigTransformation) This way it is flexible enough and does exactly what you expect it to do. For example here is the WebSiteTransformator.csproj I'm using for transforming web.config.

Here also is an example of flexibility that is impossible to reach with original WebConfigTransformation: it takes web.Template.config, applies web.$(Configuration).config over it and writes web.config. This allows us to add web.config itself into ignore list in source control. It is still valid csproj to be referenced by website:

<?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">   <PropertyGroup>     <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>     <SchemaVersion>2.0</SchemaVersion>     <OutputType>Library</OutputType>     <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>     <OutputPath>$(TEMP)\TransformWebConfig\bin</OutputPath>     <BaseIntermediateOutputPath>$(TEMP)\TransformWebConfig\obj\</BaseIntermediateOutputPath>     <IntermediateOutputPath>$(BaseIntermediateOutputPath)$(Configuration)\</IntermediateOutputPath>     <WebFolderName>$(SolutionDir)\MyWebSite\</WebFolderName>   </PropertyGroup>   <ItemGroup>     <Compile Include="Dummy.cs" />   </ItemGroup>   <UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll"/>   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />   <Target Name="BeforeBuild">     <TransformXml Source="$(WebFolderName)Web.Template.config"                   Transform="$(WebFolderName)Web.$(Configuration).config"                   Destination="$(WebFolderName)Web.config" />   </Target> </Project> 
like image 158
Andriy K Avatar answered Nov 04 '22 04:11

Andriy K