zoukankan      html  css  js  c++  java
  • Adding appsettings.json to a .NET Core console app

    This is something that strangely doesn’t seem to be that well documented and took me a while to figure out though in the end it’s pretty simple.

    All that’s required is to add the following NuGet packages and an appsettings.json file.

    • Microsoft.Extensions.Configuration
    • Microsoft.Extensions.Configuration.FileExtensions
    • Microsoft.Extensions.Configuration.Json

    The appsettings.json files “Copy to Output Directory” property should also be set to “Copy if newer” so that the application is able to access it when published.

    The settings are injected in the main method rather than in the startup method as with web apps but the code is essentially the same.

            static void Main(string[] args)
            {
                var builder = new ConfigurationBuilder()
                    .SetBasePath(Directory.GetCurrentDirectory())
                    .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
    
                IConfigurationRoot configuration = builder.Build();
    
                Console.WriteLine(configuration.GetConnectionString("Storage"));
            }
  • 相关阅读:
    gcc和g++的区别和联系
    Linux基础命令第二天
    Linux基础命令第一天
    Flask入门之完整项目搭建
    Flask入门第三天
    Flask入门第二天
    Flask入门第一天
    vue_drf之多级过滤、排序、分页
    vue_drf之视频接口
    vue_drf之支付宝接口
  • 原文地址:https://www.cnblogs.com/Javi/p/9323622.html
Copyright © 2011-2022 走看看