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

发布于 2024-10-18 09:30:32 字数 298 浏览 2 评论 0原文

似乎无法更改 Visual Studio 2010 网站的构建配置项目(与 Visual Studio Web 应用程序相反),更改构建配置是启用 Web.config 转换的关键部分(不可能将配置更改为除调试之外的任何配置)。

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

It doesn't seem to be possible to change the Build Configuration of Visual Studio 2010 Website Projects (as opposed to Visual Studio Web Applications), and changing the Build Configuration is a key part of enabling Web.config transformations (it's not possible to change the configuration to anything except Debug).

How do I get Web.config transformations to work with Visual Studio 2010 Website projects if it's not possible to change the Build Configuration?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

北斗星光 2024-10-25 09:30:33

我发现了一篇非常好的博客文章,描述了这个问题的解决方案:
http://andrewtwest.com/ 2010/02/25/using-web-config-transformations-in-web-site-projects/

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

I found a pretty good blog post describing a solution to this here:
http://andrewtwest.com/2010/02/25/using-web-config-transformations-in-web-site-projects/

In short: create an empty project (as long as it is not another website project) in your solution that contains the website. The empty project will give you access to msbuild through its project file, which will allow you to perform transforms on your website web.config.

陌伤浅笑 2024-10-25 09:30:33

我使用了一种稍微另类的方法。虽然还是有点 hack,但我认为更简单。这对我有用,但显然有很多不同的配置可用,所以我不能保证它适合每个人。这围绕着网站在发布之前首先打包到您的 AppData 文件夹中的方式...

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

    
    <配置 xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
        <应用程序设置>
            <添加 key="MySetting" value="NewValue" xdt:Transform="替换" xdt:Locator="Match(key)" />
        
        <系统.web>
            <编译 xdt:Transform="RemoveAttributes(debug)" />
        
    
    
  2. website.publishproj 文件中,确保配置设置为 Release:

    <配置条件=" '$(配置)' == '' ">发布;
    
  3. 将以下内容添加到 website.publishproj 的最底部>(就在 之前):

    <目标名称=“AfterBuild”>
      
      
    
    

I used a slightly alternative approach. Still a bit of a hack, but I think a lot more straightforward. This worked for me, but obviously there are a lot of different configurations available so I can't guarantee it'll work for everyone. This revolves around the way that a website is first packaged up in to your AppData folder before being published...

  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>
    
她比我温柔 2024-10-25 09:30:33

在 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

Create a publish profile in VS 2017 and then right click on the .pubxml profile in App_Data\PublishProfiles and select Add Config Transform.

See 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

紫轩蝶泪 2024-10-25 09:30:33

正如上面 Andriy 的评论中提到的,解决方案范围的构建事件绝对看起来是一种更干净的方式来做到这一点。

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

As mentioned in Andriy's comment above, Solution wide build events definitely seems like a cleaner way to do this.

I am adding this as a separate answer, as it gets a little lost in the comment, but IMHO is the best answer. Props to Andriy K and Sayed Ibrahim.

转瞬即逝 2024-10-25 09:30:33

如果您不想需要 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" />

改编自答案

If you would prefer not to need a Web.Template.config, I used this:

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

Adapted from an answer here.

下壹個目標 2024-10-25 09:30:32

我不想直接使用整个 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>

I'd prefer not to use entire an Web Application Project solution out of box.
My solution is to use the XmlTransform task defined in Microsoft.Web.Publishing.Tasks.dll directly (this task is the core of WebConfigTransformation)
This way it is flexible enough and does exactly what you expect it to do.
For example here is the WebSiteTransformator.csproj I'm using for transforming web.config.

Here also is an example of flexibility that is impossible to reach with original WebConfigTransformation: it takes web.Template.config, applies web.$(Configuration).config over it and writes web.config. This allows us to add web.config itself into ignore list in source control. It is still valid csproj to be referenced by website:

<?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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文