首页 > 解决方案 > 使用 Visual Studio 2019 运行应用程序时,如何防止在 Asp.Net Core API 3.1 中自动生成 web.config?

问题描述

我正在开发 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>

标签: c#asp.net-coreasp.net-core-webapiasp.net-core-3.1

解决方案


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

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

<ANCMPreConfiguredForIIS>true</ANCMPreConfiguredForIIS>

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

这是 websdk 团队现已修复的问题: https ://github.com/dotnet/websdk/issues/564#issuecomment-644199581


推荐阅读