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

查看:26
本文介绍了如何在 .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 发布配置文件(可以在 Visual Studio 的 Properties->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天全站免登陆