使用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=".\logs\stdout" hostingModel="InProcess"> <environmentVariables> <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" /> </environmentVariables> </aspNetCore> </system.webServer> </location> </configuration>