使用Visual Studio 2013发布时如何排除web.config?

时间:2015-05-04 08:02:09

标签: asp.net visual-studio visual-studio-2013

在Visual Studio 2013中使用“发布网站”功能时。如何排除发布web.config以使其不会覆盖服务器web.config?

除VS版本外,问题与以下内容相同。

How to exclude web.config when publishing with Visual Web Developer Express?

但是,其解决方案不适用于VS2013。在VS2013中找不到“构建操作”选项。设置“ExcludeFilesFromDeployment”会导致编译问题。

5 个答案:

答案 0 :(得分:28)

只需选择web.config属性并更改'构建操作'没有'没有'和'复制到输出目录'不要复制'

答案 1 :(得分:5)

我知道这是一篇老文章,有老答案,但是对我来说,两个描述的解决方案都是错误的。

Web.config风味在操作环境凭据时会带来安全风险,而“ Build Action” =“ None”解决方案则消除了在本地调试项目的可能性,因为Web.Config文件将从不出现在“ bin”目录中。

这里描述了一种更清洁的解决方案:

https://docs.microsoft.com/fr-fr/aspnet/web-forms/overview/deployment/advanced-enterprise-web-deployment/excluding-files-and-folders-from-deployment

基本上是创建一个 ProjectName .wpp.targets文件,其中包含在发布项目时要排除的文件/文件夹的列表。

要从名为 Blog 的项目中删除 Web.config ,您需要创建一个名为 Blog.wpp.targets 的文件像这样:

文件:Blog.wpp.targets

<?xml version="1.0" encoding="utf-8" ?>
<Project ToolsVersion="4.0"
         xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <ItemGroup>
    <ExcludeFromPackageFiles Include=".\Web.config">
      <FromTarget>Blog.wpp.targets</FromTarget>
    </ExcludeFromPackageFiles>
  </ItemGroup>
</Project>

无需更改项目或.csproj文件中的任何内容。

答案 2 :(得分:4)

排除web.config文件不是推荐的解决方案。您应该使用web.config转换文件,这是 web.xxx.config 文件。

Example of transformation configuration file

当您发布网站时,Visual Studio会将您的本地web.config与相应的 web.xxx.config 文件合并

配置转换文件使用XDT(XML文档转换)语法。例如,如果要更改 connectionstring ,可以在转换文件中使用这段代码:

<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <connectionStrings>
    <add name="MyDB" 
         connectionString="value for the deployed Web.config file" 
         xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
  </connectionStrings>

有关更多示例和信息,请参阅Web.config Transformation Syntax for Web Project Deployment Using Visual Studio

答案 3 :(得分:0)

2020,并使用AspCore项目,然后尝试在.csproj中添加以下代码:

<PropertyGroup>
  <TargetFramework>netcoreapp2.2</TargetFramework>
  <IsTransformWebConfigDisabled>true</IsTransformWebConfigDisabled>
</PropertyGroup>
  

请参阅https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/?view=aspnetcore-2.2#webconfig-file

答案 4 :(得分:0)

如果要从Visual Studio发布到Azure App Services(我正在使用2019),并且未将web.config的“生成操作”设置为“无”,则可以每次执行以下操作:

  1. 右键单击您的项目,选择“发布”。然后单击“预览更改”。 enter image description here

  2. 然后取消选中web.config文件,使其不会发布。您将有机会查看也会发表的内容。

enter image description here

之所以这样做,是因为我发布到多个实例,并且每个实例具有不同的版本。通过这种方式,我可以每次查看web.config,以确保是否添加了任何其他AppSettings配置,我会意识到的。