如何在Visual Studio网站项目中进行Web.config转换?

28
似乎不可能更改Visual Studio 2010网站项目的构建配置(与Visual Studio Web应用程序相对),而更改构建配置是启用Web.config转换的关键部分(不可能将配置更改为除Debug以外的任何内容)。
如果不可能更改构建配置,我该如何让Web.config转换与Visual Studio 2010网站项目一起使用?

Visual Studio的新版本支持Web Site项目中的Web.config转换。请参见下面daniel的答案,了解如何创建Web.*.config文件的说明。 - Theophilus
6个回答

9
我更倾向于不使用整个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标记的内容? - Ryan Shripat
如果你的网站项目包含.csproj文件,我猜你应该已经拥有它了。这仍然是一个外部项目,需要设置为网站的依赖项,类似于解决方案级别的网站BeforeBuild事件。而且它能够正常工作,我发誓 :) (实际上这是我当前代码库中的真实代码片段,只是将MyWebSite替换为实际文件夹名称)。 - Andriy K
2
我刚刚发现了更好的方法来创建解决方案级别的构建事件:MSBuild: 扩展解决方案构建 - Andriy K

6

这是一个不错的技巧,但我怀疑我会实施它...还是谢谢! - Ryan Shripat
2
是的,你必须采用这种hacky方法来集成msbuild,或者使用web.config转换功能,对于网站项目来说确实很糟糕。我将尽可能避免使用网站项目,并在未来使用Web应用程序项目。 - porusan

4
我采用了稍微不同的方法。这仍然有点像是一个hack,但我认为更加简单明了。这对我有用,但显然有很多不同的配置可用,所以我不能保证它适用于每个人。这围绕着网站首先被打包到您的AppData文件夹中,然后再发布...
  1. Manually add a Web.Release.config file to the website and add the necessary transforms - obviously there's no 'Add Config Transform' option for websites, hence having to do this manually. Example 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. Inside the website.publishproj file, ensure the configuration is set to Release:

    <Configuration Condition=" '$(Configuration)' == '' ">Release</Configuration>
    
  3. Add the following to the very bottom of website.publishproj (just before </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>
    

3

2
如Andriy在上面的评论中提到的那样,解决方案级别构建事件绝对是更清晰的方法来完成这个任务。
我将其作为一个独立的答案添加了进来,因为在评论中有点容易被忽略,但我认为这是最好的答案。向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" />

改编自这里的答案。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接