asp.net - 使用 Visual Studio web.config 转换进行调试

标签 asp.net visual-studio web-config visual-studio-publish

Possible Duplicate:
How can I use Web.debug.config in the built-in visual studio debugger server?

我想使用适用于发布和调试的 Web.config 转换。

当我发布 Web 应用时,Visual Studio 会根据我当前的生成配置自动转换 Web.config。 当我开始调试时,如何告诉 Visual Studio 执行相同的操作? 在调试启动时,它仅使用默认的 Web.config 而不进行转换。

有什么想法吗?

最佳答案

好的,请理解 web.debug.configweb.release.config 仅用于打包/发布。我想出了一种方法来实现您想要做的事情。我在 https://devblogs.microsoft.com/aspnet/asp-net-web-projects-web-debug-config-web-release-config/ 上写过关于它的博客。 这是摘要。

现在让我们看看如何实现提问者想要做的事情。

回顾一下,当他构建特定配置时,他希望将特定转换应用于 web.config。显然您不想维护 web.config 文件,因为它会被覆盖。

所以我们需要做的是创建一个新文件web.template.config,它只是web.config的副本。然后只需使用 Windows 资源管理器删除 web.config(不要使用 Visual Studio 删除,因为我们不想将其从项目中删除)。

注意:如果您使用的是集成到 Visual Studio 中的源代码管理提供程序,那么您可能需要从源代码管理中删除 web.config。

此外,我们不想使用 web.debug.configweb.release.config 因为它们已经在 Web 发布管道中具有明确定义的角色所以我们不想打扰这一点。因此,我们将在项目所在的同一文件夹中创建两个新文件,以及 web.template.configweb.dev.debug.configweb.config。 dev.release.config.

我们的想法是,这些将是您从 Visual Studio 调试或运行应用程序时应用的转换。现在我们需要连接到构建/打包/发布过程以将这一切连接起来。对于 Web 应用程序项目 (WAP),您可以在同一文件夹中创建一个名为 {ProjectName}.wpp.targets 的项目文件,其中 {ProjectName} 是项目的名称。如果该文件在磁盘上与 WAP 位于同一文件夹中,则它将自动导入到项目文件中。所以我创建了这个文件。我放置了以下内容:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">

  <!-- Make sure web.config will be there even for package/publish -->
  <Target Name="CopyWebTemplateConfig" BeforeTargets="Build">
    <Copy SourceFiles="web.template.config"
          DestinationFiles="web.config"/>
  </Target>
  
  <PropertyGroup>
    <PrepareForRunDependsOn>
      $(PrepareForRunDependsOn);
      UpdateWebConfigBeforeRun;
    </PrepareForRunDependsOn>
  </PropertyGroup>

  <!-- This target will run right before you run your app in Visual Studio -->
  <Target Name="UpdateWebConfigBeforeRun">
    <Message Text="Configuration: $(Configuration): web.dev.$(Configuration).config"/>
    <TransformXml Source="web.template.config"
              Transform="web.dev.$(Configuration).config"
              Destination="web.config" />
  </Target>

  <!-- Exclude the config template files from the created package -->
  <Target Name="ExcludeCustomConfigTransformFiles" BeforeTargets="ExcludeFilesFromPackage">
    <ItemGroup>
      <ExcludeFromPackageFiles Include="web.template.config;web.dev.*.config"/>
    </ItemGroup>
    <Message Text="ExcludeFromPackageFiles: @(ExcludeFromPackageFiles)" Importance="high"/>
  </Target>
</Project>

让我解释一下。我创建了 CopyWebTemplateConfig 目标,即使您没有在 Visual Studio 中调试应用程序,它也始终会在构建时将 web.template.config 复制到 web.config。 p>

这是必需的,因为我们仍然需要支持 Visual Studio 的打包/发布过程。然后,我扩展了属性 PrepareForRunDependsOn 以包含 UpdateWebConfigBeforeRun 目标。此属性用于标识在从 Visual Studio 运行任何托管项目之前需要执行的目标列表。

在此目标中,我使用 TransformXml 任务来转换 web.template.config,使用正确的 web.dev.***.config 文件。之后,您的应用程序将根据您的构建配置使用正确的 web.config 启动。 之后,我有另一个目标 ExcludeCustomConfigTransformsFiles,我通过属性 BeforeTargets=”ExcludeFilesFromPackage” 将其注入(inject)到包/发布过程中。这是必需的,因为我们不希望在打包或发布应用程序时包含这些文件。 这就是它的全部内容。

更多地解释此场景的打包/发布过程。当您打包/发布 web.debug.configweb.release.config 时,根据构建配置,仍将使用。但最终它要转换的文件是 web.template.config,因此您可能需要根据该文件中的内容进行调整。有问题/意见吗?

关于asp.net - 使用 Visual Studio web.config 转换进行调试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3922291/

相关文章:

c# - 与 Windows 应用程序的客户端通信

javascript - 限制用户在动态生成的文本框中输入特定字符

c - 具有返回类型 typedef 结构的函数在 VS2015 中不起作用

visual-studio-2010 - 在 Visual Studio 中发布事件之后

visual-studio - 在将 NuGet 安装为 VS 扩展的情况下重新安装 NuGet 包

.net - web.config 超时和 IIS 超时有什么区别?

asp.net - 来自 url 的 SPA 不在 root 上(并且包括 filename.aspx)路由和刷新

asp.net - 错误 HTTP 404.11 通过查询字符串传递帐户激活 token

c# - 如何从代码中获取 customErrors 的 defaultRedirect 值?

iis - 部署到 IIS 会删除 URL 重写规则吗?