首页 文章

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

提问于
浏览
27

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

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

5 回答

  • 1

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

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

  • 4

    我不想完全使用整个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>
    
  • 8

    我使用了一种略微替代的方法 . 仍然有点黑客,但我认为更直接 . 这对我有用,但显然有很多不同的配置可用,所以我可以为每个人工作 . 这是围绕网站首次打包到您的 AppData 文件夹之前的方式...

    • 手动将 Web.Release.config 文件添加到网站并添加必要的转换 - 显然网站有's no '添加配置转换选项,因此必须手动执行此操作 . 示例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>
    
    • website.publishproj 文件中,确保配置设置为Release:
    <Configuration Condition=" '$(Configuration)' == '' ">Release</Configuration>
    
    • 将以下内容添加到 website.publishproj 的最底部(就在 </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>
    
  • 2

    正如Andriy上面的评论中提到的那样,Solution wide build events看起来似乎是一种更清洁的方式 .

    我将此作为一个单独的答案添加,因为它在评论中有点迷失,但恕我直言是最好的答案 . Andriy K和赛义德易卜拉欣的道具 .

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

相关问题