ASP.NET Core 2.0系列学习笔记-配置文件
2018-02-26 23:28
906 查看
ASP.NET Core 2.0 配置文件:
应用程序的配置文件:appsettings.json,引用前端的包文件:bower.json,打包配置文件:bundleconfig.json。
ASP.NET Core 2.0 MVC下默认appsettings.json :
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
}
}bundleconfig.json:// Configure bundling and minification for the project.
// More info at https://go.microsoft.com/fwlink/?LinkId=808241 [
{
"outputFileName": "wwwroot/css/site.min.css",
// An array of relative input file paths. Globbing patterns supported
"inputFiles": [
"wwwroot/css/site.css"
]
},
{
"outputFileName": "wwwroot/js/site.min.js",
"inputFiles": [
"wwwroot/js/site.js"
],
// Optionally specify minification options
"minify": {
"enabled": true,
"renameLocals": true
},
// Optionally generate .map file
"sourceMap": false
}
]
读取配置文件(appsettings.json )
应用程序的配置文件:appsettings.json,引用前端的包文件:bower.json,打包配置文件:bundleconfig.json。
ASP.NET Core 2.0 MVC下默认appsettings.json :
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
}
}bundleconfig.json:// Configure bundling and minification for the project.
// More info at https://go.microsoft.com/fwlink/?LinkId=808241 [
{
"outputFileName": "wwwroot/css/site.min.css",
// An array of relative input file paths. Globbing patterns supported
"inputFiles": [
"wwwroot/css/site.css"
]
},
{
"outputFileName": "wwwroot/js/site.min.js",
"inputFiles": [
"wwwroot/js/site.js"
],
// Optionally specify minification options
"minify": {
"enabled": true,
"renameLocals": true
},
// Optionally generate .map file
"sourceMap": false
}
]
读取配置文件(appsettings.json )
{ "connectionStrings": { "conn": "Data Source=localhost;Initial Catalog=helloworld;Integrated Security=True" }, "appSettings": { "app_key": "helloworld", "app_secret": "1234567890abcdef" }, "Logging": { "IncludeScopes": false, "LogLevel": { "Default": "Warning" } } }
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; //IConfigurationRoot ConfigurationBuilder using Microsoft.Extensions.Logging; using NLog;//NLog日志配置 namespace NETCoreNLog { public class Program { public static void Main(string[] args) { BuildWebHost(args).Run(); ConfigAndLog(); } public static IWebHost BuildWebHost(string[] args) => WebHost.CreateDefaultBuilder(args) .UseStartup<Startup>() .Build(); public static IConfigurationRoot Configuration { get; set; } public static Logger log = LogManager.GetCurrentClassLogger(); public static void ConfigAndLog() { var builder = new ConfigurationBuilder() .SetBasePath(Directory.GetCurrentDirectory()) .AddJsonFile("appsettings.json"); Configuration = builder.Build(); string app_key = Configuration["appSettings:app_key"]; string conn = Configuration["connectionStrings:conn"]; log.Debug($"数据库连接为:{conn}"); } } }
相关文章推荐
- ASP.NET Core 2.0系列学习笔记-NLog日志配置文件
- [dotnetCore2.0]学习笔记之二: ASP.NET Core中,如何灵活使用静态文件和加载自定义配置
- ASP.NET Core 2.0系列学习笔记-ORM框架之Dapper
- ASP.NET Core 2.0系列学习笔记-DI依赖注入
- ASP.NET Core 2.0系列学习笔记-启动类Startup
- ASP.NET Core 2.0系列学习笔记-Middleware中间件
- ASP.NET Core 2.0系列学习笔记-ADO.NET-MSSQLServerHelper
- ASP.NET Core 2.0系列学习笔记-应用程序修改默认端口支持外网IP访问
- Quartz.NET 2.0 学习笔记(3) :通过配置文件实现任务调度
- ASP.NET 3.5核心编程学习笔记(9):用户配置文件
- C#编译器优化那点事 c# 如果一个对象的值为null,那么它调用扩展方法时为甚么不报错 webAPI 控制器(Controller)太多怎么办? .NET MVC项目设置包含Areas中的页面为默认启动页 (五)Net Core使用静态文件 学习ASP.NET Core Razor 编程系列八——并发处理
- 学习ASP.NET Core Razor 编程系列十五——文件上传功能(三)
- 学习ASP.NET Core Razor 编程系列十三——文件上传功能(一)
- Quartz.NET 2.0 学习笔记(3) :通过配置文件实现任务调度
- 2.2Bind建立配置文件和实体的映射「深入浅出ASP.NET Core系列」
- asp.net core1.x/asp.net core2.0中如何加载多个配置文件
- Asp.net core 学习笔记 ( upload/download files 文件上传与下载 )
- 学习ASP.NET Core Razor 编程系列十四——文件上传功能(二)
- Quartz.NET 2.0 学习笔记(3) :通过配置文件实现任务调度
- asp.net core2.0中网站发布的时候,怎么样才配置才可以使视图文件不被打包进去?