如何防止在 Asp.Net Core API 3.1 中自动生成 web.config 而 运行 使用 visual studio 2019 的应用程序?

How to prevent auto-generated web.config in Asp.Net Core API 3.1 while running the application using visual studio 2019?

我正在开发 Asp.Net Core 3.1 API,它按预期工作。

当我 运行 时,它会自动将原始 web.config 转换为新的。

原始Web.config文件:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath="dotnet" arguments=".\EngageAPI.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess" />
    </system.webServer>
  </location>
</configuration>

新建web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess">
        <environmentVariables>
          <environmentVariable name="COMPLUS_ForceENC" value="1" />
          <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
        </environmentVariables>
      </aspNetCore>
    </system.webServer>
  </location>
</configuration>

所以我有两个问题。

  1. 为什么它会自动生成?
  2. 如何停止自动生成?

更新:

为问题添加更多说明:

为什么会导致这个问题?我正在从 azure dev ops 进行部署。我所做的是将 web.config 从我的项目复制到我正在创建的包中。并注意到当您 运行 来自 VS 的代码时,此 web.config 被修改为新的 web.config(其中有 %LAUNCHER_PATH% %LAUNCHER_ARGS%)。所以基本上我们不需要复制这个web.config。而不是这个,我们需要复制在发布文件夹中生成的那个。

CsProj 文件

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <Folder Include="Models\" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="7.0.0" />
    <PackageReference Include="Dapper" Version="2.0.35" />
    <PackageReference Include="Microsoft.Azure.Storage.Blob" Version="11.1.7" />
    <PackageReference Include="Microsoft.Azure.Storage.Common" Version="11.1.7" />
    <PackageReference Include="Microsoft.Extensions.Logging.Log4Net.AspNetCore" Version="3.1.0" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.3" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="5.5.0" />
    <PackageReference Include="Swashbuckle.AspNetCore.Filters" Version="5.1.1" />
    <PackageReference Include="System.Data.SqlClient" Version="4.8.1" />
  </ItemGroup>


</Project>

似乎您在调试时使用的是 IIS 而不是 IIS express。

尝试将其添加到您的 .csproj 文件中:

<ANCMPreConfiguredForIIS>true</ANCMPreConfiguredForIIS>

还要确保你的 VS 版本 >= VS 16.6.3

这个问题现在已经被 websdk 团队修复了: https://github.com/dotnet/websdk/issues/564#issuecomment-644199581

Visual Studio会根据你的launchsettings.json自动更新web.config文件。当您从 IDE run/debug 或使用 dotnet 运行 时,它会这样做。因此,从 net6.0 和 VS2022 开始的答案是修改您的启动设置配置文件,以便它停止更改您的文件。

这是一个例子

 "IIS Express": {
  "commandName": "IISExpress",
  "launchBrowser": true,      
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development"
  },
  "ancmHostingModel": "OutOfProcess"
},

processPath 是自动计算的,但我相信可以指定您自己的。进程托管模型也可以在这里设置。

也许您希望每个开发人员都有自己的 launchsettings.json 以适应他们的环境。

当您发布项目时,它会再次改变您的 web.config。如果您知道您拥有此 .csproj 属性

的所有正确设置,则可以禁用此行为
<IsWebConfigTransformDisabled>true</ IsWebConfigTransformDisabled>

web.config 可以在 build/publish 时通过转换进行控制,但是当 running/debugging 来自 Visual Studio(或 dotnet 运行)时,它将始终覆盖您的转换基于启动设置:)