它是如何工作的Web.config转换?

时间:2012-04-20 15:50:03

标签: asp.net visual-studio-2010 web-config transformation slowcheetah

我不太确定它是如何运作的。

我的意思是,当我创建部署包时,Web配置已更改,但我怀疑是这个。

我有2个webConfig转换文件:

web.debug.config web.release.config

这些转换文件是可用的,还是只在我们制作webDeploy或制作部署包时才有效?当从Visual Studio(web developer express)运行它的项目时,使用根Web.config。

我的意思是,简而言之,当项目从visual studio运行时,web.config转换不起作用?

4 个答案:

答案 0 :(得分:4)

你是对的。

部署或运行​​部署程序包时将应用配置转换。

它们不会对编译进行转换。

答案 1 :(得分: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标记以获取所有必需的配置文件。此外,这可以在构建之前或之后完成。

答案 2 :(得分:1)

您可以使用MSBuild和名为SlowCheetah的扩展程序调用它。

答案 3 :(得分:0)

还有另一个名为Configuration Transform的VS扩展程序。 如果您不想安装它,但要实现此目的,只需按照演示解决方案中显示的示例添加不同的构建配置文件,并在项目文件中添加一些新的MSBuild任务。演示解决方案的The download link可以在扩展程序的Visual Studio库网页上找到。由于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.pubxml ProjectFolder / Properties / PublishProfiles / Publish.pubxml)进行FileSystem发布,因此默认情况下会发生web.config转换。以下是样本

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