发布后的项目打开swagger

使用netcore作为纯后端提供api已经变得越来越频繁,swagger也成为很多人的选择。通常会在代码中限制ASPNETCORE_ENVIRONMENT为Production时关闭swagger。但是往往我们需要将api发布到本地iis调试或供他人使用时,swagger将会被禁止。发布后项目往往默认为Production环境,将其修改为Development即可解决。

解决方法

打开发布到iis的文件夹下的web.config文件,添加以下代码:

<environmentVariables>
     <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
</environmentVariables>

修改后的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="*.dll" stdoutLogEnabled="false" stdoutLogFile=".logsstdout" hostingModel="InProcess">
      <environmentVariables>
        <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
      </environmentVariables>
    </aspNetCore>
    </system.webServer>
  </location>
</configuration>
原文地址:https://www.cnblogs.com/sylone/p/11563815.html