Adding appsettings.json to a .NET Core console app
2018-07-17 15:31
501 查看
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")); }
相关文章推荐
- 阿里云容器服务与ASP.NET Core部署:用 docker secrets 保存 appsettings.Production.json
- ASP .NET CORE 根据环境变量支持多个 appsettings.json
- Talking appsettings.json in Asp.Net Core
- ASP .NET CORE 根据环境变量支持多个 appsettings.json
- [转]Setting the NLog database connection string in the ASP.NET Core appsettings.json
- How to set custom JsonSerializerSettings for Json.NET in MVC 4 Web API?
- ABP .Net Core To Json序列化配置
- Asp.Net Core 中无法使用 ConfigurationManager.AppSettings
- asp.net core web app build success but fail to start up
- How to use Bundle&Minifier and bundleconfig.json in ASP.NET Core
- ADO.NET .net core2.0添加json文件并转化成类注入控制器使用 简单了解 iTextSharp实现HTML to PDF ASP.NET MVC 中 Autofac依赖注入DI 控制反转IOC 了解一下 C# AutoMapper 了解一下
- 解决ASP.NET错误 Failed to map the path '/App_GlobalResources/'
- java.lang.ClassCastException: libcore.net.http.HttpURLConnectionImpl cannot be cast to javax.net.ssl
- 解决ASP.NET MVC(post数据)Json请求太大,无法反序列化(The JSON request was too large to be deserialized)
- ASP.NET Core WebApi 返回统一格式参数(Json 中 Null 替换为空字符串)
- ASP.NET 2.0 遍历配置文件中的appSettings connectionStrings
- ASP.NET Core 返回 Json DateTime 格式
- How to embed Excel in a .NET WinForms App
- 在net中json序列化与反序列化 面向对象六大原则 (第一篇) 一步一步带你了解linq to Object 10分钟浅谈泛型协变与逆变
- ASP.NET Core 1.0: Deploy to IIS