0

Swagger 已添加到 WebApi。为了避免 Swagger 在生产中可用,在 Startup.cs 中进行了检查。当您将部署的引用从 .dll 更改为 .exe 时,这可以正常工作。但是,当将其保留在 .dll 时,Swagger 仍然可用。为什么要更改配置以及如何防止这种情况发生?

发布个人资料:

<?xml version="1.0" encoding="utf-8"?>
<!--
This file is used by the publish/package process of your Web project. You can customize the behavior of this process
by editing this MSBuild file. In order to learn more about this please visit https://go.microsoft.com/fwlink/?LinkID=208121. 
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <WebPublishMethod>FileSystem</WebPublishMethod>
    <PublishProvider>FileSystem</PublishProvider>
    <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    <SiteUrlToLaunchAfterPublish />
    <LaunchSiteAfterPublish>False</LaunchSiteAfterPublish>
    <PrecompileBeforePublish>True</PrecompileBeforePublish>
    <EnableUpdateable>False</EnableUpdateable>
    <DebugSymbols>False</DebugSymbols>
    <LastUsedPlatform>.NET</LastUsedPlatform>
    <WDPMergeOption>MergeAllOutputsToASingleAssembly</WDPMergeOption>
    <UseMerge>True</UseMerge>
    <SingleAssemblyName>WebApi.Published</SingleAssemblyName>
    <ExcludeApp_Data>False</ExcludeApp_Data>
    <ProjectGuid>2603565c-93cb-4c9f-bae2-86889931c11e</ProjectGuid>
    <publishUrl>..\..\Publish\WebApi\Release</publishUrl>
    <DeleteExistingFiles>True</DeleteExistingFiles>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <SelfContained>false</SelfContained>
    <EnvironmentName>Production</EnvironmentName>
  </PropertyGroup>
</Project>

源代码中的配置:

<?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=".\WebApi.exe" arguments="" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess">
        <environmentVariables>
          <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
          <environmentVariable name="ASPNETCORE_HTTPS_PORT" value="443" />
          <environmentVariable name="COMPLUS_ForceENC" value="1" />
        </environmentVariables>
      </aspNetCore>
    </system.webServer>
  </location>
</configuration>

发布的配置:

<?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=".\WebApi.dll" arguments="" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="inprocess">
        <environmentVariables>
          <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Production" />
          <environmentVariable name="ASPNETCORE_HTTPS_PORT" value="443" />
          <environmentVariable name="COMPLUS_ForceENC" value="1" />
        </environmentVariables>
      </aspNetCore>
    </system.webServer>
  </location>
</configuration>
4

1 回答 1

-1

我最终在构建后事件中复制了正确的 web.config:

  <Target Name="PostBuild" AfterTargets="PostBuildEvent">
    <Exec Command="copy &quot;$(ProjectDir)Web.$(ConfigurationName).Config&quot; &quot;$(TargetDir)Web.Config&quot; />
  </Target>

因为:

<IsTransformWebConfigDisabled>True</IsTransformWebConfigDisabled>

Web.Config 不会随发布自动复制。但需要此命令以避免将 .exe 更改为 .dll。因此,另外添加了一个命令,因此发布也复制了 Web.Config:

  <Target Name="CopyWebConfig" AfterTargets="Publish">
    <Copy SourceFiles="$(TargetDir)\Web.Config" DestinationFiles="$(PublishDir)\Web.Config"/>
  </Target>
于 2020-11-26T14:56:33.570 回答