Visual studio 如何对Visual Studio网站项目进行Web.config转换?
似乎不可能(与Visual Studio Web应用程序相反),更改生成配置是启用Web.config转换的关键部分(不可能将配置更改为除调试之外的任何内容)Visual studio 如何对Visual Studio网站项目进行Web.config转换?,visual-studio,visual-studio-2010,web-config,Visual Studio,Visual Studio 2010,Web Config,似乎不可能(与Visual Studio Web应用程序相反),更改生成配置是启用Web.config转换的关键部分(不可能将配置更改为除调试之外的任何内容) 如果无法更改生成配置,如何使Web.config转换与Visual Studio 2010网站项目一起工作?我在这里找到了一篇很好的博客文章,介绍了解决方案: 简而言之:在解决方案中创建一个包含该网站的空项目(只要它不是另一个网站项目)。空项目将允许您通过其项目文件访问msbuild,这将允许您在网站web.config上执行转换。我不
如果无法更改生成配置,如何使Web.config转换与Visual Studio 2010网站项目一起工作?我在这里找到了一篇很好的博客文章,介绍了解决方案:
简而言之:在解决方案中创建一个包含该网站的空项目(只要它不是另一个网站项目)。空项目将允许您通过其项目文件访问msbuild,这将允许您在网站web.config上执行转换。我不希望使用现成的整个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>
任意CPU
2
图书馆
v4.0
$(临时)\TransformWebConfig\bin
$(临时)\TransformWebConfig\obj\
$(baseIntermediateOutput路径)$(配置)\
$(SolutionDir)\My网站\
正如安德烈在上面的评论中所提到的,这显然是一种更干净的方法
我将此作为一个单独的答案添加,因为它在评论中有点迷失,但IMHO是最好的答案。安德烈·K和赛义德·易卜拉欣的道具。如果您不希望需要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" />
$([System.IO.Path]::GetTempFileName())
$([System.IO.Path]::GetTempFileName())
根据答案改编。我使用了一种略微不同的方法。还是有点老套,但我觉得要简单得多。这对我很有效,但显然有很多不同的配置,所以我不能保证它对每个人都有效。这是围绕网站在发布之前首先打包到您的
AppData
文件夹的方式展开的
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>
网站的最底部。publishproj
(就在
之前):
在VS 2017中创建发布配置文件,然后右键单击App_Data\PublishProfiles中的.pubxml配置文件并选择添加配置转换
看这是一个很好的技巧,但我怀疑我是否能实现它。。。无论如何谢谢你!是的,你不得不求助于这样一种骇人的方法来集成msbuild,或者甚至使用web.config转换特性来集成网站项目,这真是太糟糕了。我正在避免网站项目,并将在未来尽可能多地使用web应用程序项目。我需要对此进行测试,这非常有趣!你是说我只需要在我的网站项目的.csproj中添加类似于UsingTask和TransformXml标记的东西?如果你的网站项目包含.csproj,我想你应该开箱即用。这仍然是要设置为网站依赖项的外部项目,类似于构建事件之前的解决方案级别网站。它是有效的,我发誓:)(实际上这是我当前代码库的真实部分,只是用MyWebSite而不是真正的文件夹名)。我刚刚找到了制作解决方案范围内构建事件的更好方法:较新版本的Visual Studio支持网站项目中的Web.config转换。有关如何创建Web.*.config文件的说明,请参见下文。
<Configuration Condition=" '$(Configuration)' == '' ">Release</Configuration>
<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>