web.config根据环境变量合并配置文件
在csproj工程项目文件里配置
<Target Name="ApplyXdtConfigTransform" BeforeTargets="Compile;Link"> <Copy SourceFiles="appsettings.dev.json" DestinationFiles="appsettings.$(Configuration.ToLower()).json" Condition="!Exists('appsettings.$(Configuration.ToLower()).json')" /> <Copy SourceFiles="nlog.$(Configuration.ToLower()).config" DestinationFiles="nlog.config" Condition="Exists('nlog.$(Configuration.ToLower()).config')" /> <Copy SourceFiles="nlog.template.config" DestinationFiles="nlog.config" Condition="!Exists('nlog.$(Configuration.ToLower()).config')" OverwriteReadOnlyFiles="true" /> <PropertyGroup> <_SourceWebConfig>web.template.config</_SourceWebConfig> <_XdtWebConfigTransform>web.$(Configuration.ToLower()).config</_XdtWebConfigTransform> <_TargetWebConfig>web.config</_TargetWebConfig> </PropertyGroup> <Copy SourceFiles="web.dev.config" DestinationFiles="$(_XdtWebConfigTransform)" Condition="!Exists('$(_XdtWebConfigTransform)')" /> <Exec Command="dotnet transform-xdt --xml "$(_SourceWebConfig)" --transform "$(_XdtWebConfigTransform)" --output "$(_TargetWebConfig)"" Condition="Exists('$(_XdtWebConfigTransform)')" /> </Target>
web.template.config
<?xml version="1.0" encoding="utf-8"?> <configuration> <!-- Configure your application settings in appsettings.json. Learn more at http://go.microsoft.com/fwlink/?LinkId=786380 --> <system.webServer> <security> <requestFiltering> <!-- This will handle requests up to 200MB --> <requestLimits maxAllowedContentLength="204800000" /> </requestFiltering> </security> <modules runAllManagedModulesForAllRequests="true"> <remove name="WebDAVModule" /> </modules> <handlers> <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/> </handlers> <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\App_Data\logs\stdout" forwardWindowsAuthToken="false"> <environmentVariables> <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="debug" /> </environmentVariables> </aspNetCore> </system.webServer> </configuration>
web.release.config
<?xml version="1.0" encoding="utf-8"?> <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> <system.webServer> <aspNetCore> <environmentVariables> <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="release" xdt:Transform="SetAttributes" xdt:Locator="Match(name)" /> </environmentVariables> </aspNetCore> </system.webServer> </configuration>

浙公网安备 33010602011771号