【问题标题】:How do I do Web.config Transformations with Visual Studio Website Projects?如何使用 Visual Studio 网站项目进行 Web.config 转换?
【发布时间】:2011-02-22 18:16:55
【问题描述】:

change the Build Configuration of Visual Studio 2010 Website Projects 似乎不可能(与 Visual Studio Web 应用程序相反),更改构建配置是启用 Web.config 转换的关键部分(不可能将配置更改为任何内容调试除外)。

如果无法更改构建配置,如何让 Web.config 转换与 Visual Studio 2010 网站项目一起使用?

【问题讨论】:

  • 较新版本的 Visual Studio 支持网站项目中的 Web.config 转换。有关如何创建 Web.*.config 文件的说明,请参阅下面的 daniel's answer

标签: visual-studio visual-studio-2010 web-config


【解决方案1】:

我不希望开箱即用地使用整个 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>

【讨论】:

  • 我需要测试一下,这很有趣!所以你是说我只需要在我的网站项目的 .csproj 中添加类似于 UsingTask 和 TransformXml 标签的东西?
  • 如果您的网站项目包含 .csproj,我想您应该开箱即用。这仍然是要设置为网站依赖项的外部项目,一种解决方案级别的网站 BeforeBuild 事件。它有效,我发誓:) (实际上这是我当前代码库的真正部分,只是使用 MyWebSite 而不是真正的文件夹名称)。
  • 我刚刚找到了制作解决方案范围构建事件的更好方法:MSBuild: Extending the solution build
【解决方案2】:

我在这里找到了一篇很好的博客文章,描述了一个解决方案: http://andrewtwest.com/2010/02/25/using-web-config-transformations-in-web-site-projects/

简而言之:在包含该网站的解决方案中创建一个空项目(只要它不是另一个网站项目)。空项目将允许您通过其项目文件访问 msbuild,这将允许您在您的网站 web.config 上执行转换。

【讨论】:

  • 这是一个很好的 hack,但我怀疑我是否会实施它......无论如何,谢谢!
  • 是的,你不得不求助于这种 hacky 的方法来集成 msbuild,甚至使用 web.config 转换功能,与网站项目,这非常糟糕。我正在避免网站项目,并在未来尽可能多地使用 Web 应用程序项目。
【解决方案3】:

我使用了一种稍微不同的方法。仍然有点hack,但我认为更简单。这对我有用,但显然有很多不同的配置可用,所以我不能保证它对每个人都有效。这与网站在发布之前首先打包到您的AppData 文件夹中的方式有​​关...

  1. 手动将Web.Release.config 文件添加到网站并添加必要的转换 - 显然网站没有“添加配置转换”选项,因此必须手动执行此操作。示例 Web.Release.config:

    <?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 的最底部(就在&lt;/Project&gt; 之前):

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

【讨论】:

    【解决方案4】:

    在 VS 2017 中创建发布配置文件,然后右键单击 App_Data\PublishProfiles 中的 .pubxml 配置文件并选择添加配置转换。

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/31f41991-abb2-41de-ad0b-c1379cc7c806/vs-2013-express-for-web-webconfig-transforms?forum=visualstudiogeneral&prof=required

    【讨论】:

      【解决方案5】:

      正如上面 Andriy 的评论中提到的,Solution wide build events 绝对看起来是一种更清洁的方法。

      我将此添加为单独的答案,因为它在评论中有点丢失,但恕我直言是最好的答案。给 Andriy K 和 Sayed Ibrahim 的道具。

      【讨论】:

        【解决方案6】:

        如果您不想使用 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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-08-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多