0

我不太確定它是如何工作的。它是如何工作的Web.config轉換?

我的意思是,當我創建一個部署包時,web配置發生了變化,但我的疑問是這樣的。

我有2個webConfig轉換文件:

web.debug.config web.release.config

這些轉換文件只是提供或當我們做一個webDeploy或作出部署包只是工作?當它從Visual Studio運行的項目(web developer express)使用根Web.config時。

我的意思是說,當項目從Visual Studio運行時,web.config轉換不起作用?

回答

1

你可以使用MSBuild和名爲SlowCheetah的擴展來調用它。

+0

我嘗試,但它似乎不與視覺工作室Web開發人員明確表達我得到這個錯誤:「此擴展是不可安裝任何當前安裝的產品」。 – Allende 2012-04-20 16:08:52

+0

即使使用SlowCheetah,對於Web項目,文件僅在包/發佈時進行轉換。 – 2012-04-28 02:36:27

+0

Web Developer Express不支持擴展,您需要付費版本的Visual Studio擴展。 – 2012-05-02 06:31:10

4

你是對的。

在部署或運行​​部署程序包時應用配置轉換。

它們不會在編譯時轉換。

4

如果在編譯過程中需要轉換的配置文件,可以通過編輯項目文件(.csproj)並添加下面的代碼來獲取它。

<Target Name="AfterBuild"> 
    <TransformXml Source="$(SolutionDir)WCFServices\Web.config" 
        Transform="$(SolutionDir)WCFServices\Web.Release.config" 
        Destination="$(OutDir)WebRelease.config" 
        StackTrace="true" /> 
</Target> 

可以添加多個TransformXml標籤以獲取所有需要的配置文件。另外,這可以在構建之前或之後完成。

+0

謝謝@Charls!我不知道。 – Allende 2012-05-02 00:58:28

0

還有一個叫做Configuration Transform的VS擴展很適合這個。 如果您不想安裝它,但要實現這個,只需按照演示解決方案中顯示的示例添加不同的構建配置文件,並在項目文件中添加一些新的MSBuild任務。演示解決方案的The download link可在擴展的Visual Studio Gallery網頁上找到。這種方法不需要任何額外的包,因爲MSBuild使用XSLT來執行XML轉換。

下面是從演示解決方案添加到項目文件中的MSBuild任務。就我而言,我跟着它的VS2015的ASP.NET MVC項目,我沒有把<UsingTask TaskName="TransformXml" AssemblyFile=...

<UsingTask TaskName="TransformXml" AssemblyFile="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Web\Microsoft.Web.Publishing.Tasks.dll" /> 
    <Target Name="AfterCompile" Condition="Exists('App.$(Configuration).config')"> 
    <!--Generate transformed app config in the intermediate directory--> 
    <TransformXml Source="App.config" Destination="$(IntermediateOutputPath)$(TargetFileName).config" Transform="App.$(Configuration).config" /> 
    <!--Force build process to use the transformed configuration file from now on.--> 
    <ItemGroup> 
     <AppConfigWithTargetPath Remove="App.config" /> 
     <AppConfigWithTargetPath Include="$(IntermediateOutputPath)$(TargetFileName).config"> 
     <TargetPath>$(TargetFileName).config</TargetPath> 
     </AppConfigWithTargetPath> 
    </ItemGroup> 
    </Target> 
    <!--Override After Publish to support ClickOnce AfterPublish. Target replaces the untransformed config file copied to the deployment directory with the transformed one.--> 
    <Target Name="AfterPublish"> 
    <PropertyGroup> 
     <DeployedConfig>$(_DeploymentApplicationDir)$(TargetName)$(TargetExt).config$(_DeploymentFileMappingExtension)</DeployedConfig> 
    </PropertyGroup> 
    <!--Publish copies the untransformed App.config to deployment directory so overwrite it--> 
    <Copy Condition="Exists('$(DeployedConfig)')" SourceFiles="$(IntermediateOutputPath)$(TargetFileName).config" DestinationFiles="$(DeployedConfig)" /> 
    </Target> 

這是我在.csproj的文件應用的方式,很簡單:

<Target Name="AfterBuild" Condition="Exists('Web.$(Configuration).config')"> 
    <Exec Command="attrib -R Web.config" /> 
    <TransformXml Source="Web.config" Transform="Web.$(Configuration).config" Destination="Web.config" StackTrace="true" /> 
    </Target> 

此外還有一個很好的post

進一步,爲web.config中轉型,Since VS2012我們可以添加一個發佈配置 - Publish.pubxmlProjectFolder /Properties/PublishProfiles/Publish.pubxml)做文件系統發佈,因此網絡。那麼默認會發生配置轉換。下面是一個樣本

<?xml version="1.0" encoding="utf-8"?> 
    <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> 
    <PropertyGroup> 
     <WebPublishMethod>FileSystem</WebPublishMethod> 
     <SiteUrlToLaunchAfterPublish /> 
     <publishUrl Condition="$(OutDir) != ''">$(OutDir)\_PublishedWebsites\$(ProjectName)</publishUrl> <!-- For MSBuild --> 
     <publishUrl Condition="$(OutDir) == ''">$(MSBuildThisFileDirectory)..\..\_PublishedWebsite\</publishUrl> <!-- For Visual Studio...cant use $(ProjectName) --> 
     <DeleteExistingFiles>True</DeleteExistingFiles> 
    </PropertyGroup> 
    </Project>