发布时转换或替换 Visual Studio 中的 html 文件

Transform or replace html file in Visual Studio on publish

我在项目中有一个静态 HTML 文件,当我 publish/build 解决方案时,我想 transform/replace 它。这是否可以在不向文件本身添加任何服务器端代码的情况下实现?

假设我的应用程序根目录中有一个 HTML 文件:

www.example.com/serverinfo.html

文件将包含静态内容:

SERVER1;www.example.com

但是当我在本地 运行 时,我希望它能够渲染

SERVER1;www.example.local

该文件应该可以在没有任何服务器端处理的情况下访问,并且不能包含任何代码。我习惯了 web.config 转换。这是否也可以应用于 HTML 文件?还是仅适用于 web.config?

我想要一个类似于 web.config 的结构:

serverinfo.html
serverinfo.html.debug
serverinfo.html.release

我使用的是 Visual Studio 2015,通常部署到 Azure。我现在可以手动将静态文件添加到服务器,但我真的很想在解决方案中管理它。

我确定有更简单的方法,但我最终制作了一个构建脚本。

  <Target Name="ApplySpecificProxy" Condition="Exists('$(MSBuildProjectDirectory)\..\build\proxy\serverinfo.$(Configuration).html')" AfterTargets="BeforeBuild">
    <Message Text='Copying Proxy.html' Importance="high" />
    <Copy SourceFiles="$(MSBuildProjectDirectory)\..\build\proxy\proxy.$(Configuration).html"
          DestinationFiles="$(MSBuildProjectDirectory)\serverinfo.html" ContinueOnError="false"  />
  </Target>