.net core appsetting/获取配置文件
2024-09-01 03:33:53
修改appsetting
最近用Identity4所以需要做一个配置项项目
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
},
"Indentity": {"Center": "",//这部分就是要取的
"ApiCenter": ""
}
}
在startup中获取
首先我们要从 Indentity中 获取center当中的数据
这部分比较简单只要在startup 中的ConfigureServices 获取 Indentity下的Center
this.Configuration["Indentity:Center"]
就这么一句话就可以获取到了
将内容注入到管道中
1.创建一个model
public class IndentitySettingModel
{
public string Center { get; set; }
public string ApiCenter { get; set; }
}
2.在startup中注入到管道当中
services.Configure<Utility.HelperModel.IndentitySettingModel>(this.Configuration.GetSection("Indentity"));
兄弟。这样就注入到管道当中了。接下来我们就可以去controller中获取 了
在controller中获取
/// <summary>
/// 获取AccessToken
/// </summary>
/// <returns></returns>
public string GetAccessToken(IOptions<IndentitySettingModel> settings)
{
return settings.Center;
}
恩很 就是这样 就可以获取的到了。
最详细的config引用
刚才有几个朋友和我说 。我干的很漂亮纯属糊弄人的文章。
好的。 我重新写一下吧。
我把所有需要引用DLL 给大家贴图吧。太多了。
算了,还是打出来吧。
Microsoft.Extensions.Configuration
Microsoft.Extensions.Configuration.Abstractions
Microsoft.Extensions.Configuration.Json
Microsoft.Extensions.DependencyInjection
Microsoft.Extensions.Options
Microsoft.Extensions.Options.ConfigurationExtensions
把这些 nuget引用
编写Help类
public class ConfigurationUtil
{ public static readonly IConfiguration Configuration; static ConfigurationUtil()
{
Configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", true)
.Build();
} public static T GetSection<T>(string key) where T : class, new()
{
var obj = new ServiceCollection()
.AddOptions()
.Configure<T>(Configuration.GetSection(key))
.BuildServiceProvider()
.GetService<IOptions<T>>()
.Value;
return obj;
} public static string GetSection(string key)
{
return Configuration.GetValue<string>(key);
}
}
调用方法
ConfigurationUtil.GetSection("MongoDb:ConnectionString")
骂人部分
天天让我写。天天没人点赞!
后续
创建了一个QQ群希望有志之士可以加一下
点击链接加入群聊【.Net Core研究团】:https://jq.qq.com/?_wv=1027&k=5298dNv
最新文章
- java md5
- 原生Ajax封装随笔
- Matlab画图,坐标轴范围设置和间隔设置
- android自定义控件(7)-获取自定义ImageView的src属性
- Android NDK开发入门实例
- MYSQL例题合集
- Android中XML格式数据的简单使用
- opencv 2.46与visual studio 2012 配置方法
- redis存储session配制方法
- 一道经典的C++结构体的题目
- Delphi遍历文件夹及子文件夹(可查找固定格式文件)
- jQuery事件触发和参数传递
- eclipse启动tomcat不能访问解决
- leetcode 169 Majority Element 冰山查询
- SSM-Spring-12:Spring中NameMatchMethodPointcutAdvisor名称匹配方法切入点顾问
- 基本数据对象(int,float,str)
- ClickHouse之访问权限控制
- 【Linux】创建不可修改文件
- Jquery easyui 重置按钮,easyui 清空表单,Jquery easyui 重置表单
- ubuntu16.4中开启vncserver进行远程桌面