2011-02-22 91 views

回答

5

我發現了一個相當不錯的博客中描述瞭解決這一位置: http://andrewtwest.com/2010/02/25/using-web-config-transformations-in-web-site-projects/

總之:創建一個空的項目(只要它不是另一個網站項目),其中包含該網站在您的解決方案。空白的項目會讓你通過它的項目文件訪問msbuild,這將允許你在你的網站web.config上執行轉換。

+0

這是一個很好的黑客,但我懷疑我會執行它... 不管怎麼說,還是要謝謝你! – 2011-02-23 13:39:46

+2

是的,它非常糟糕,你必須訴諸這樣一個hacky的方法來整合msbuild,甚至使用web.config轉換功能,與網站項目。我正在避免網站項目,並儘可能在將來使用網絡應用程序項目。 – porusan 2011-02-23 17:43:12

+0

同樣在這裏....... – 2011-02-24 13:06:27

7

我不想使用整個Web應用程序項目解決方案。 我的解決方案是直接使用Microsoft.Web.Publishing.Tasks.dll中定義的XmlTransform任務(此任務是WebConfigTransformation的核心) 這種方式非常靈活,並且完全符合您的期望。 例如,這裏是我用於轉換web.config的WebSiteTransformator.csproj。

這裏還有一個原始WebConfigTransformation無法實現的靈活性示例:它需要web.Template.config,在其上應用web. $(Configuration).config並寫入web.config。這使我們可以將web.config本身添加到源代碼管理中的忽略列表中。它仍然是有效的csproj通過網站引用:

<?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> 
2

正如舍甫琴科的評論如上所述,Solution wide build events肯定似乎是一個更清潔的方式來做到這一點。

我加入這個作爲一個單獨的答案,因爲它在評論中有點失落,但恕我直言是最好的答案。道具Andriy K和Sayed Ibrahim。

1

如果您不希望需要Web.Template.config,我用這個:

<PropertyGroup> 
    <_tempSourceFile>$([System.IO.Path]::GetTempFileName())</_tempSourceFile> 
    <_tempTransformFile>$([System.IO.Path]::GetTempFileName())</_tempTransformFile> 
</PropertyGroup> 

<Copy SourceFiles="$(ProjectDir)Web.config" DestinationFiles="$(_tempSourceFile)"/> 
<Copy SourceFiles="$(ProjectDir)Web.$(Configuration).config" DestinationFiles="$(_tempTransformFile)"/> 

<TransformXml Source="$(_tempSourceFile)" 
       Transform="$(_tempTransformFile)" 
       Destination="$(ProjectDir)Web.config" 
       StackTrace="false" /> 

從一個答案here改編。

3

我使用了一個稍微不同的方法。還有點破解,但我覺得更簡單一些。這對我很有用,但顯然有很多不同的配置可用,所以我不能保證它適用於所有人。這是圍繞一個網站被公佈之前,首先打包到您的AppData文件夾的方式......

  1. 手動一個Web.Release.config文件添加到網站,並添加必要的變換 - 顯然沒有「添加配置轉換「選項的網站,因此不得不手動這樣做。示例Web.Release。配置:

    <?xml version="1.0" encoding="utf-8"?> 
    <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> 
        <appSettings> 
         <add key="MySetting" value="NewValue" xdt:Transform="Replace" xdt:Locator="Match(key)" /> 
        </appSettings> 
        <system.web> 
         <compilation xdt:Transform="RemoveAttributes(debug)" /> 
        </system.web> 
    </configuration> 
    
  2. 裏面的website.publishproj文件,確保配置設置爲Release:

    <Configuration Condition=" '$(Configuration)' == '' ">Release</Configuration> 
    
  3. 添加以下的website.publishproj(只是</Project>之前)最底部:

    <Target Name="AfterBuild"> 
        <MakeDir Directories="$(PackageArchiveRootDir)\..\CSAutoParameterize\original" /> 
        <TransformXml Source="Web.config" Transform="Web.$(ConfigurationName).config" Destination="$(PackageArchiveRootDir)\..\CSAutoParameterize\original\Web.config" StackTrace="false" /> 
    </Target> 
    
相關問題