9

我在 Visual Studio 2013 ASP.NET Web API 2 项目中有以下设置。

  • Web.Develop.config Web 转换以设置应用程序设置键值
  • Web.Release.config Web 转换以删除应用设置键
  • Develop.pubxml 映射到 Web.Develop.config 转换
  • Release.pubxml 映射到 Web.Release.config 转换

每个细节都在下面找到。

<!-- Web.Develop.config (Web Config Transform) -->
<appSettings>
  <add key="ReportInputPath" 
       value="DevelopPath" 
       xdt:Transform="SetAttributes" 
       xdt:Locator="Match(key)" />
  </appSettings>
<!-- Web.Release.config (Web Config Transform) -->
<appSettings xdt:Transform="Remove" />
<!-- **Develop.pubxml (Publish Profile) -->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <WebPublishMethod>FileSystem</WebPublishMethod>
    <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
    <LastUsedPlatform>x64</LastUsedPlatform>
    <SiteUrlToLaunchAfterPublish />
    <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
    <ExcludeApp_Data>True</ExcludeApp_Data>
    <publishUrl>Path</publishUrl>
    <DeleteExistingFiles>True</DeleteExistingFiles
    <ExcludeFilesFromDeployment>packages.config</ExcludeFilesFromDeployment>
  </PropertyGroup>
</Project>
<!-- Release.pubxml (Publish Profile) -->
<!-- Contents are identical to Develop.pubxml. 
     This is used to target the Web.Release.Config transform. -->

每当我通过发布发布配置文件发布应用程序时,我的<appSettings/>元素都会成功删除。但是,<appSettings/>当开发发布配置文件也运行时,元素也会被删除。

我想了解的是:

为什么<appSettings/>在我运行开发发布配置文件而不是设置 ReportInputPath 值时会删除元素?

解决方案/项目配置、发布配置文件和 web.config 转换之间的关系是什么?

4

1 回答 1

10

为什么在运行开发发布配置文件时删除元素的答案<appSettings/>是因为两个转换按以下顺序运行。

  1. Web.Release.config。这是因为 Develop.pubxml 文件中的配置目标是 Release 构建配置而运行的。
  2. Web.开发.配置。这是因为发布配置文件 (Develop) 的名称与转换文件的名称匹配。

发生的事情是第一个转换删除了<appSettings/>元素。第二个转换尝试在该元素中设置键值,但找不到它,因此它静默失败。

我可以通过搜索控制台输出来确认这一点。运行开发转换时,会出现一条警告,指出找不到所需的元素。

Example (shortened for clarity)
> TransformXml: Applying Transform File: C:\...\MyProject\Web.Develop.config
> C:\...\MyProject\Web.Develop.config(6,4): Warning : No element in the source document matches '/configuration/appSettings'
> TransformXml: Not executing SetAttributes (transform line 9, 10)

Sayed Ibrahim Hashimi 撰写的Profile specific web.config 转换和转换预览文章非常有助于确定这是问题所在。

至于构建配置、发布配置文件和 web.config 转换之间的关系,我目前的理解是这样的。

  1. 发布配置文件具有(除其他外)配置目标
  2. 发布配置文件首先运行映射到其指定配置目标名称的转换(如果存在)
  3. 发布配置文件然后运行映射到其发布配置文件名称的转换(如果存在)

这里的关键是可以运行两个web.config 转换。

于 2015-07-31T02:15:59.310 回答