mvc_core_config

*:first-child {
margin-top: 0 !important;
}

body>*:last-child {
margin-bottom: 0 !important;
}

/* BLOCKS
=============================================================================*/

p, blockquote, ul, ol, dl, table, pre {
margin: 15px 0;
}

/* HEADERS
=============================================================================*/

h1, h2, h3, h4, h5, h6 {
margin: 20px 0 10px;
padding: 0;
font-weight: bold;
-webkit-font-smoothing: antialiased;
}

h1 tt, h1 code, h2 tt, h2 code, h3 tt, h3 code, h4 tt, h4 code, h5 tt, h5 code, h6 tt, h6 code {
font-size: inherit;
}

h1 {
font-size: 28px;
color: #000;
}

h2 {
font-size: 24px;
border-bottom: 1px solid #ccc;
color: #000;
}

h3 {
font-size: 18px;
}

h4 {
font-size: 16px;
}

h5 {
font-size: 14px;
}

h6 {
color: #777;
font-size: 14px;
}

body>h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h4:first-child, body>h5:first-child, body>h6:first-child {
margin-top: 0;
padding-top: 0;
}

a:first-child h1, a:first-child h2, a:first-child h3, a:first-child h4, a:first-child h5, a:first-child h6 {
margin-top: 0;
padding-top: 0;
}

h1+p, h2+p, h3+p, h4+p, h5+p, h6+p {
margin-top: 10px;
}

/* LINKS
=============================================================================*/

a {
color: #4183C4;
text-decoration: none;
}

a:hover {
text-decoration: underline;
}

/* LISTS
=============================================================================*/

ul, ol {
padding-left: 30px;
}

ul li > :first-child,
ol li > :first-child,
ul li ul:first-of-type,
ol li ol:first-of-type,
ul li ol:first-of-type,
ol li ul:first-of-type {
margin-top: 0px;
}

ul ul, ul ol, ol ol, ol ul {
margin-bottom: 0;
}

dl {
padding: 0;
}

dl dt {
font-size: 14px;
font-weight: bold;
font-style: italic;
padding: 0;
margin: 15px 0 5px;
}

dl dt:first-child {
padding: 0;
}

dl dt>:first-child {
margin-top: 0px;
}

dl dt>:last-child {
margin-bottom: 0px;
}

dl dd {
margin: 0 0 15px;
padding: 0 15px;
}

dl dd>:first-child {
margin-top: 0px;
}

dl dd>:last-child {
margin-bottom: 0px;
}

/* CODE
=============================================================================*/

pre, code, tt {
font-size: 12px;
font-family: Consolas, "Liberation Mono", Courier, monospace;
}

code, tt {
margin: 0 0px;
padding: 0px 0px;
white-space: nowrap;
border: 1px solid #eaeaea;
background-color: #f8f8f8;
border-radius: 3px;
}

pre>code {
margin: 0;
padding: 0;
white-space: pre;
border: none;
background: transparent;
}

pre {
background-color: #f8f8f8;
border: 1px solid #ccc;
font-size: 13px;
line-height: 19px;
overflow: auto;
padding: 6px 10px;
border-radius: 3px;
}

pre code, pre tt {
background-color: transparent;
border: none;
}

kbd {
-moz-border-bottom-colors: none;
-moz-border-left-colors: none;
-moz-border-right-colors: none;
-moz-border-top-colors: none;
background-color: #DDDDDD;
background-image: linear-gradient(#F1F1F1, #DDDDDD);
background-repeat: repeat-x;
border-color: #DDDDDD #CCCCCC #CCCCCC #DDDDDD;
border-image: none;
border-radius: 2px 2px 2px 2px;
border-style: solid;
border-width: 1px;
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
line-height: 10px;
padding: 1px 4px;
}

/* QUOTES
=============================================================================*/

blockquote {
border-left: 4px solid #DDD;
padding: 0 15px;
color: #777;
}

blockquote>:first-child {
margin-top: 0px;
}

blockquote>:last-child {
margin-bottom: 0px;
}

/* HORIZONTAL RULES
=============================================================================*/

hr {
clear: both;
margin: 15px 0;
height: 0px;
overflow: hidden;
border: none;
background: transparent;
border-bottom: 4px solid #ddd;
padding: 0;
}

/* TABLES
=============================================================================*/

table th {
font-weight: bold;
}

table th, table td {
border: 1px solid #ccc;
padding: 6px 13px;
}

table tr {
border-top: 1px solid #ccc;
background-color: #fff;
}

table tr:nth-child(2n) {
background-color: #f8f8f8;
}

/* IMAGES
=============================================================================*/

img {
max-width: 100%
}
-->

在.net core mvc中 配置文件格式更改为json且移除了ConfigurationManager.AppSettings[xmlkey]的方法,
那么,如何来获取配置信息呢?

第一步 将json文件添加到应用程序中

<default code>
public static void Main(string[] args)
{
BuildWebHost(args).Run();
} public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build(); 如果是使用默认的方法来启动的,则可以跳过此步 <reason>
查看源码 在 CreateDefaultBuilder方法中已使用添加配置文件 //配置信息处理 -- 用于第二步的读取配置信息
.ConfigureAppConfiguration((Action<WebHostBuilderContext, IConfigurationBuilder>) ((hostingContext, config) =>
{
//注入运行环境
IHostingEnvironment hostingEnvironment = hostingContext.HostingEnvironment;
//加载配置文件
config.AddJsonFile("appsettings.json", true, true).AddJsonFile(string.Format("appsettings.{0}.json", (object) hostingEnvironment.EnvironmentName), true, true);
//根据运行环境加载相应文件
if (hostingEnvironment.IsDevelopment())
{
Assembly assembly = Assembly.Load(new AssemblyName(hostingEnvironment.ApplicationName));
if (assembly != (Assembly) null)
config.AddUserSecrets(assembly, true);
} config.AddEnvironmentVariables();
if (args == null)
return;
config.AddCommandLine(args);
})) 自定义添加配置文件请参考以上代码

第二步 读取配置信息

<code>

    <Startup>

    //由于在程序启动时已经配置了configuration 则可以直接通过构造方法获取配置信息

    public Startup(IConfiguration configuration)
{
Configuration = configuration;
} public IConfiguration Configuration { get; }

第三步 在Controller 获取配置信息

<code>

    <Startup>

        <method --> ConfigureServices>

        //添加选项
services.AddOptions(); //将配置信息进行DI注入
services.Configure<AppSetting>(Configuration.GetSection(nameof(AppSetting))); <相关方法说明>
GetSection:
key-value 取值
类似于Dictionary取值的操作 Configure:
进行DI注入 关键性代码:
//进行DI注入 AddSingleton 全局共享一个 IOptionsChangeTokenSource<TOptions> 注入参数对应的type
services.AddSingleton<IConfigureOptions<TOptions>>((IConfigureOptions<TOptions>) new NamedConfigureFromConfigurationOptions<TOptions>(name, config)) 即实际上还是利用AddSingleton注入实现 <source code>
/// <summary>
/// Registers a configuration instance which TOptions will bind against. 将配置实例作为option绑定到参数上
/// </summary>
/// <typeparam name="TOptions">The type of options being configured.</typeparam>
/// <param name="services">The <see cref="T:Microsoft.Extensions.DependencyInjection.IServiceCollection" /> to add the services to.</param>
/// <param name="config">The configuration being bound.</param>
/// <returns>The <see cref="T:Microsoft.Extensions.DependencyInjection.IServiceCollection" /> so that additional calls can be chained.</returns>
public static IServiceCollection Configure<TOptions>(this IServiceCollection services, IConfiguration config) where TOptions : class
{
return services.Configure<TOptions>(Microsoft.Extensions.Options.Options.DefaultName, config);
} /// <summary>
/// Registers a configuration instance which TOptions will bind against.同上
/// </summary>
/// <typeparam name="TOptions">The type of options being configured.</typeparam>
/// <param name="services">The <see cref="T:Microsoft.Extensions.DependencyInjection.IServiceCollection" /> to add the services to.</param>
/// <param name="name">The name of the options instance.</param>
/// <param name="config">The configuration being bound.</param>
/// <returns>The <see cref="T:Microsoft.Extensions.DependencyInjection.IServiceCollection" /> so that additional calls can be chained.</returns>
public static IServiceCollection Configure<TOptions>(this IServiceCollection services, string name, IConfiguration config) where TOptions : class
{
if (services == null)
throw new ArgumentNullException(nameof (services));
if (config == null)
throw new ArgumentNullException(nameof (config));
//进行DI注入 AddSingleton 全局共享一个 IOptionsChangeTokenSource<TOptions> 注入参数对应的type
services.AddSingleton<IOptionsChangeTokenSource<TOptions>>((IOptionsChangeTokenSource<TOptions>) new ConfigurationChangeTokenSource<TOptions>(name, config));
return services.AddSingleton<IConfigureOptions<TOptions>>((IConfigureOptions<TOptions>) new NamedConfigureFromConfigurationOptions<TOptions>(name, config));
} <Controller>
public abstract class BaseController : Controller
{ protected AppSetting AppSetting { get; set; } protected BaseController(IOptions<AppSetting> option) => AppSetting = option.Value; } intro:由于在mvc经常会使用配置信息,于是我便将获取配置信息的相关处理封装在一个抽象控制器中,故只需要在使用配置信息的控制器中继承此控制器即可 然后就可以在控制器中通过AppSetting获取配置信息了

扩展说明

在Startup中直接获取配置文件:

public Startup(IApplicationEnvironment appEnv, IHostingEnvironment env) {

   _config = new ConfigurationBuilder()
.AddJsonFile("config.json")
.AddJsonFile("appsettings.json")
.AddEnvironmentVariables()
.Build();
} [https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/configuration/?view=aspnetcore-2.1&tabs=basicconfiguration](https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/configuration/?view=aspnetcore-2.1&tabs=basicconfiguration "官方教程")

补充说明:

看见很多人说配置信息的获取不能实时更新,其实只是应用方式错了罢了

通过查看源码可以知道

《source code》
services.AddSingleton<IOptionsChangeTokenSource<TOptions>>((IOptionsChangeTokenSource<TOptions>) new ConfigurationChangeTokenSource<TOptions>(name, config)); 微软实际上是有采用修改监听的 故只需要将我们控制构造中的IOptions<> 替换为 IOptionsMonitor<>即可实时读取配置信息 //读取最新值
IOptionsMonitor<>.CurrentValue

IOptionsMonitor

定义:解析
//获取最新值
TOptions CurrentValue { get; } //通过given name获取值
TOptions Get(string name); //当值发送改变时触发
IDisposable OnChange(Action<TOptions, string> listener); 通过定义可知IOptionsMonitor是一个类似于监听器的存在
所以应该就是通过这个来实时进行更新的

备注:其中使用的AppSetting 即 我配置信息对应的实体类 【可根据实际情况进行命名】

欢迎各位大佬评论并指出我的错误 :)

author:monster

since:6/4/2018 11:12:45 AM

direction:.net core 2.0 mvc 配置文件的使用

.net core 2.0 mvc 获取配置信息的更多相关文章

  1. ASP.NET Core 2.0 MVC - 获取当前登录用户信息

    一.前言 上篇实战完成后,没想到会有那么多的圈友给了那么多的支持,甚至连只是作为代码仓储的git上也给了一些小星星,真的感觉很惶恐啊,哈哈哈,毕竟代码写的很烂啊.由于上一篇只是大概说了下项目,所以准备 ...

  2. .Net Standard(.Net Core)实现获取配置信息

    一.前言 在.Net Framework框架有专门获取webconfig配置的方法供我们使用,但是在.Net Core或者.Net Standard中没有可以直接使用的方法来获取配置文件信息,下面就来 ...

  3. asp.net core 3.0 MVC JSON 全局配置

    asp.net core 3.0 MVC JSON 全局配置 System.Text.Json(default) startup配置代码如下: using System.Text.Encodings. ...

  4. .net core 2.0 mvc 初步学习

    mvc_study *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !impor ...

  5. JavaWeb学习之Servlet(四)----ServletConfig获取配置信息、ServletContext的应用

    [声明] 欢迎转载,但请保留文章原始出处→_→ 文章来源:http://www.cnblogs.com/smyhvae/p/4140877.html [正文] 一.ServletConfig:代表当前 ...

  6. ASP.NET Core 2.0 MVC项目实战

    一.前言 毕业后入职现在的公司快有一个月了,公司主要的产品用的是C/S架构,再加上自己现在还在学习维护很老的delphi项目,还是有很多不情愿的.之前实习时主要是做.NET的B/S架构的项目,主要还是 ...

  7. (转)JavaWeb学习之Servlet(四)----ServletConfig获取配置信息、ServletContext的应用

    [声明] 欢迎转载,但请保留文章原始出处→_→ 文章来源:http://www.cnblogs.com/smyhvae/p/4140877.html [正文] 一.ServletConfig:代表当前 ...

  8. ASP.NET CORE 1.0 MVC API 文档用 SWASHBUCKLE SWAGGER实现

    from:https://damienbod.com/2015/12/13/asp-net-5-mvc-6-api-documentation-using-swagger/ 代码生成工具: https ...

  9. c#实现Google账号登入授权(OAuth 2.0)并获取个人信息

    c#实现Google账号登入授权(OAuth 2.0)并获取个人信息   此博主要介绍通过google 账号(gmail)实现登入,授权方式OAuth2.0,下面我们开始介绍. 1.去google官网 ...

随机推荐

  1. 搭建一个Web API项目(DDD)

    传送阵:写在最后 一.创建一个能跑的起来的Web API项目 1.建一个空的 ASP.NET Web应用 (为什么不直接添加一个Web API项目呢,那样会有些多余的内容(如js.css.Areas等 ...

  2. 【312】◀▶ arcpy 常用函数说明

    其他常用的 ArcPy 函数说明 序号 类名称   功能说明   语法 & 举例 01 RefreshActiveView   ====<<<< Description ...

  3. 解决Eclipse编辑JavaScript时卡的问题

    eclipse在开发JavaEE项目时容易卡,特别是在编辑JavaScript时,经过网上各种搜索,综合整理一下,对自己的eclipse设置之后,结果不在出现卡的问题了. 原文地址:http://bl ...

  4. lucene3.0范围查找

    在lucene3.0以上版本中,范围查询也有很大的变化,RangeQuery已经不推荐使用,使用TermRangeQuery和NumericRangeQuery两个替代.TermRangeQuery: ...

  5. 设置Windows开机自动启动VirtualBox虚拟机系统

    如果常用VirtualBox虚拟机系统的话,设置随开机启动也是很方便的.不需要打开VirtualBox窗口,直接启动VirtualBox虚拟机系统就可以了. 设置开机自启动VirtualBox虚拟机系 ...

  6. 数据库连接池--druid

    数据库连接池常用的有:dbcp,c3p0,druid 代码仓库(https://github.com/) package com.huawei.test; import java.sql.Connec ...

  7. 数组和集合(四)、Map集合的使用总结

    一.概述 键值对,无序 键唯一.值不唯一 只允许存在一个Key为null元素 二.实现类 1. HashMap · 无序,数组+链表+红黑树 · 非线程安全 2. LinkedHashMap · 有序 ...

  8. Spring中Aspect的切入点的表达式定义细节

    用过很多次切面aspect了,对于表达式总是记得很模糊,今天总结一下. 1.切面做如下设置则只会拦截返回值为String类型的方法 @Aspect public class MyInterceptor ...

  9. iOS设备尺寸

  10. C++ 模板 与 泛型编程

    C++ 模板 与 泛型编程 前言 模板有两种:类模板和函数模板 .模板是泛型编程的基础. 什么叫:泛型编程? 使用独立于特定类型的方式进行编程.也就是我们在编程的时候不明确的写上类型,而是使用一个模板 ...