如何在.Net核心应用程序中发布特定于环境的应用程序设置? [英] How to publish environment specific appsettings in .Net core app?

查看:95
本文介绍了如何在.Net核心应用程序中发布特定于环境的应用程序设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的.Net核心应用程序中有3个特定于环境的 appsettings 文件

I have 3 environment specific appsettings files in my .Net core application

project.json 中,我已经设置了 publishOptions 像这样。 (基于建议此处

in project.json I have setup publishOptions like this. ( based on suggestion here)

"publishOptions": {
    "include": [
      "wwwroot",      
      "appsettings.development.json",
      "appsettings.staging.json",
      "appsettings.production.json",
      "web.config"
    ]
  },

我有3个相应的启动类,它们使用适当的 appsettings 基于环境

I have 3 corresponding startup classes that uses appropriate appsettings based on environment

var builder = new ConfigurationBuilder()
    .SetBasePath(env.ContentRootPath)
    .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: false, reloadOnChange: true);

但是,当我发布应用程序时,所有3个appsettings文件最终都会出现在所有环境中。如何发布环境特定的appsetting文件?

However when I publish the application then all 3 appsettings files end up in all environments. How do I publish environment specific appsetting file?

推荐答案

如果其他人想知道如何在多个环境中使用不同的appsettings,

If someone else is wondering how to use different appsettings for multiple environments here is a possible solution.

dotnet publish --configuration [Debug | Release] 将适当的appsettings.json文件复制到如果 *。csproj 具有以下文件的条件逻辑,则为发布文件夹:

dotnet publish --configuration [Debug|Release] will copy the appropriate appsettings.json file into the publish folder if *.csproj has a conditional logic for these files:


  • 首先在 .pubxml 发布配置文件中(可以在属性-> PublishProfiles ),默认情况下禁用所有内容文件

  • First in the .pubxml publish profile file (can be found in Properties->PublishProfiles of Visual Studio) disable that all content files are included by default
<PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <EnableDefaultContentItems>false</EnableDefaultContentItems>
</PropertyGroup>




  • 然后指定条件调试/释放逻辑

  • <Choose>
        <When Condition="'$(Configuration)' == 'Debug'">
          <ItemGroup>
            <None Include="appsettings.json" CopyToOutputDirectory="Always" CopyToPublishDirectory="Always" />
            <None Include="appsettings.prod.json" CopyToOutputDirectory="Never" CopyToPublishDirectory="Never" />
          </ItemGroup>
        </When>
        <When Condition="'$(Configuration)' == 'Release'">
          <ItemGroup>
            <None Include="appsettings.json" CopyToOutputDirectory="Never" CopyToPublishDirectory="Never" />
            <None Include="appsettings.prod.json" CopyToOutputDirectory="Always" CopyToPublishDirectory="Always" />
          </ItemGroup>
        </When>
    </Choose>
    




    • 最后位于 Startup.cs 尝试加载两个文件

      • Finally inside Startup.cs try to load both files
      • public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile($"appsettings.prod.json", optional: true, reloadOnChange: true)
                .AddJsonFile($"appsettings.json", optional: true, reloadOnChange: true)
                .AddEnvironmentVariables();
        
            Configuration = builder.Build();
        }
        

        我希望此解决方案会有所帮助。

        I hope this solution, has been helpful.

        这篇关于如何在.Net核心应用程序中发布特定于环境的应用程序设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆