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

Rya*_*pat 27 web-config visual-studio-2010 visual-studio

似乎无法更改Visual Studio 2010网站项目的构建配置(与Visual Studio Web应用程序相对),更改构建配置是启用Web.config转换的关键部分(无法更改)除了Debug之外的任何配置).

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

And*_*y K 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>
Run Code Online (Sandbox Code Playgroud)

  • 我刚刚找到了更好的方法来制作解决方案范围的构建事件:[MSBuild:扩展解决方案构建](http://sedodream.com/2010/10/22/MSBuildExtendingTheSolutionBuild.aspx) (2认同)

por*_*san 6

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

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

  • 是的,你必须采用这种hacky方法来集成msbuild,甚至将web.config转换功能与网站项目结合使用,这非常糟糕.我正在避免网站项目,并且将来尽可能多地使用Web应用程序项目. (2认同)