介紹
使用netcore作為純后端提供api已經變得越來越頻繁,swagger也成為很多人的選擇。通常會在代碼中限制ASPNETCORE_ENVIRONMENT為Production時關閉swagger。但是往往我們需要將api發布到本地iis調試或供他人使用時,swagger將會被禁止。發布后項目往往默認為Production環境,將其修改為Development即可解決。
解決方法
打開發布到iis的文件夾下的web.config文件,添加以下代碼:
1 <environmentVariables> 2 <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" /> 3 </environmentVariables>
修改后的web.config結構大致如下:
1 <?xml version="1.0" encoding="utf-8"?> 2 <configuration> 3 <location path="." inheritInChildApplications="false"> 4 <system.webServer> 5 <handlers> 6 <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" /> 7 </handlers> 8 <aspNetCore processPath="dotnet" arguments="*.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="InProcess"> 9 <environmentVariables> 10 <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" /> 11 </environmentVariables> 12 </aspNetCore> 13 </system.webServer> 14 </location> 15 </configuration> 16 <!--ProjectGuid: 15af0485-b65a-422a-bf12-5877b85abb6c-->
更多環境及發布詳細介紹可參考netcore環境設置。