ASP.NET Core appsettings.json文件(9)《从零开始学ASP.NET CORE MVC》:
本文出自《从零开始学ASP.NET CORE MVC》
推荐文章:ASP.NET Core launchsettings.json文件
ASP.NET Core appsettings.json文件
在本视频中,我们将讨论ASP.NET Core 项目中appsettings.json
文件的重要性。
在以前的ASP.NET版本中,我们将应用程序配置设置(例如数据库连接字符串)存储在web.config
文件中。
在 Asp.Net Core 中, 应用程序配置设置可以来自以下不同的配置源。
文件(appsettings.json, appsettings.{Environment}.json)
Environment
环境不同,托管在对应环境。User secrets (用户机密)
Environment variables (环境变量)
Command-line arguments (命令行参数)
appsettings.json f文件: 我们的项目是通过Asp.net Core 预制的"空"模板创建的,所以我们的项目中已经有一个appsettings.json 的文件了。
我们可以对文件进行如下修改,补充一个MyKey
的键值对:
{"Logging": {"LogLevel": {"Default": "Warning" } },"AllowedHosts": "*","MyKey": " appsettings.json中Mykey的值",}
访问配置信息
若要访问 "Startup " 类中的配置信息, 请注入框架提供的 IConfiguration
服务。Startup
类位于 startup. cs 文件中。
public class Startup{private IConfiguration _configuration;
// 注意,我们在这里使用了依赖注入public Startup(IConfiguration configuration){ _configuration = configuration; }
public void ConfigureServices(IServiceCollection services){ }
public void Configure(IApplicationBuilder app, IHostingEnvironment env){if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); }
app.Run(async (context) => { await context.Response.WriteAsync(_configuration["MyKey"]); }); }}
依赖注入
在以前版本的ASP.NET中,依赖注入是可选的,要配置它,我们必须使用像Ninject,autofac、castle windsor等第三方框架。
在 asp. net Core 中, 依赖注入是不可或缺的一部分。依赖注入能使我们能够创建低耦合、可扩展且易于测试的系统。
我们将在即将推出的视频中详细讨论依赖注入,尽情期待。
ASP.NET Core IConfiguration 服务
IConfiguration
服务是为了从asp.net Core 中的所有各种配置源读取配置信息而设计的。如果在多个配置源中具有相同密钥名称的配置设置,简单来说就是重名了,则后面的配置源将覆盖先前的配置源 。
几个地方的演示,分别是如何替换的。
launchsetting
静态类
WebHost
的CreateDefaultBuilder()
方法在应用程序启动时会自动去调用,按特定顺序读取配置源。要查看配置源的读取顺序,请查看以下链接上的
ConfigureAppConfiguration()
方法
https://github.com/aspnet/MetaPackages/blob/release/2.2/src/Microsoft.AspNetCore/WebHost.cs
检查文件后,您将看到,以下是读取各种配置源的默认顺序
appsettings.json,
appsettings.{Environment}.json
用户机密
环境变量
5.命令行参数
如果您想要改变他们的调用顺序,甚至往里面添加属于自己的自定义配置信息,我们将在后面的课程中讨论如何自定义配置源。
小结
所以翻源代码也没有那么可怕嘛
/// <summary> /// Initializes a new instance of the <see cref="WebHostBuilder"/> class with pre-configured defaults using typed Startup. /// </summary> /// <remarks> /// The following defaults are applied to the returned <see cref="WebHostBuilder"/>: /// use Kestrel as the web server and configure it using the application's configuration providers, /// set the <see cref="IHostingEnvironment.ContentRootPath"/> to the result of <see cref="Directory.GetCurrentDirectory()"/>, /// load <see cref="IConfiguration"/> from 'appsettings.json' and 'appsettings.[<see cref="IHostingEnvironment.EnvironmentName"/>].json', /// load <see cref="IConfiguration"/> from User Secrets when <see cref="IHostingEnvironment.EnvironmentName"/> is 'Development' using the entry assembly, /// load <see cref="IConfiguration"/> from environment variables, /// load <see cref="IConfiguration"/> from supplied command line args, /// configure the <see cref="ILoggerFactory"/> to log to the console and debug output, /// enable IIS integration. /// </remarks> /// <typeparam name ="TStartup">The type containing the startup methods for the application.</typeparam> /// <param name="args">The command line args.</param> /// <returns>The initialized <see cref="IWebHostBuilder"/>.</returns> public static IWebHostBuilder CreateDefaultBuilder<TStartup>(string[] args) where TStartup : class => CreateDefaultBuilder(args).UseStartup<TStartup>();
硬广专区
如果您觉得我的文章质量还不错,欢迎打赏,也可以订阅我的视频哦
未得到授权不得擅自转载本文内容,52abp.com保留版权
文字版目录: https://www.52abp.com/Wiki/mvc/latest/1.Intro
代码托管地址:https://gitee.com/aiabpedu
知乎专栏:https://zhuanlan.zhihu.com/52abp交流QQ群:952387474《微软MVP带你学ASP.NET CORE》
【收费】腾讯课堂: https://ke.qq.com/course/392589?tuin=2522cdf3
【免费】youtube视频专区:http://t.cn/Ei0F2EB
【免费】B站: https://space.bilibili.com/2954671
免费的更新慢,收费的更新快,仅此而已。就这样。
「好看」的人都【在看】↓↓↓
ASP.NET Core appsettings.json文件(9)《从零开始学ASP.NET CORE MVC》:相关推荐
- ASP.NET Core launchsettings.json文件(8)《从零开始学ASP.NET CORE MVC》:
本文出自<从零开始学ASP.NET CORE MVC> 推荐文章:ASP.NET Core 进程外(out-of-process)托管 ASP.NET Core launchsetting ...
- ASP.NET Core 进程外(out-of-process)托管(7)《从零开始学ASP.NET CORE MVC》
本文出自<从零开始学ASP.NET CORE MVC> 推荐文章:ASP.NET Core 进程内(InProcess)托管 ASP.NET Core 进程内(InProcess)托管 我 ...
- ASP.NET Core 进程内(InProcess)托管(6)《从零开始学ASP.NET CORE MVC》:
本文出自<从零开始学ASP.NET CORE MVC> 推荐文章:ASP.NET Core 中的 Main方法 ASP.NET Core 进程内(InProcess)托管 在这个视频中我们 ...
- 《从零开始学ASP.NET CORE MVC》:ASP.NET Core 中的 Main方法(5)
本文出自<从零开始学ASP.NET CORE MVC> 推荐文章:ASP.NET Core Web 项目文件 ASP.NET Core 中的 Main方法 一个开始专心写字的人 在ASP. ...
- 《从零开始学ASP.NET CORE MVC》课程介绍
大家好,欢迎来到52ABP学院,收看我们的 <从零开始学ASP.NET CORE MVC>. ASP.NET Core 简介 从2015年开始随时互联网成长,云计算和AI.大数据的爆发,大 ...
- NET问答: 如何在 ASP.NET Core 的 .json 文件中读取 AppSettings ?
咨询区 Oluwafemi: 在 appsettings.json 中我有如下的 AppSettings 实体数据,如下代码所示: {"AppSettings": {"t ...
- 从零开始学ASP.NET(基础篇)
原作者:蓝鲸 出处:5D多媒体 学ASPNET与ASP有区别,这种区别不是语言上的,而是思路上的区别.ASP是纯面向过程的,而ASPNET是完全面向对向的.这种区别使我们在编程的结构设计上要与ASP有 ...
- 78. Spring Boot完美使用FastJson解析JSON数据【从零开始学Spring Boot】
[原创文章,转载请注明出处] 个人使用比较习惯的json框架是fastjson,所以spring boot默认的json使用起来就很陌生了,所以很自然我就想我能不能使用fastjson进行json解析 ...
- 从零开始学ASP.NET
2010年,微软推出了Visual Studio 2010开发工具,增强了ASP.NET网络应用方面的技术,提供了ASP.NET 4.0版本,为开发人员带来很多便捷.本书基于最新的ASP.NET版本, ...
最新文章
- android 课程——样式
- LeakCanary(一)使用篇
- Android应用中网络请求库Volley的使用
- linux下创建用户及组
- Qt5 中 QWebEngineView 的使用,让桌面客户端和 web 端友好通信
- 【小技巧】【Java】 创建指定数目m的Set数组
- Polo the Penguin and Matrix
- 系统架构师学习笔记-数据通信与计算机网络(一)
- 考研 | 手把手教你打赢考研情报战,巧用有关考研的三大网站
- 包裹点云位姿估计_基于点云位姿平均的非合作目标三维重构
- 计算机网络技术期中,计算机网络技术基础期中试卷
- 排序专题之C++中的sort函数调用
- Python3 找不到库
- 程序员面试金典——4.5检查是否为BST
- 给android studio安装新字体,如mac系统的monaco字体
- python基础函数应用_python基础之函数的应用
- 微信小程序 【给图片加上删除图标】
- 基于java+swing的物业收费管理系统(java+swing+Gui)
- 自定义关机计算机,win7自定义定时关机设置方法是什么
- 奇葩算法系列——猴子排序
热门文章
- 影响程序员生涯的三个错误观念,你千万不要犯!
- 反射调用 java bean的set和get方法
- POJ 3080 Blue Jeans (后缀数组)
- AM335x kernel4.4.12 LCD 时钟翻转设置记录
- java web移植 遇到Project facet Java version 1.7 is not supported
- iOS-Runtime知识点整理
- [LeetCode]119.Pascal#39;s Triangle II
- 话里话外:企业管理软件的方案设计要规避哪些风险
- 编写properties文件的Eclipse插件
- session、cookie、隐藏域、url参数传递四种会话及跟踪方式