netcore获取配置文件的内容
2024-10-12 12:47:52
结合了好几个哥们的写法,最终我使用了这个版本,现在把我这个版本的写出来,如果该版本和您的版本类似或者一样,那可能是借鉴您的
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
using System;
using System.Collections.Generic;
using System.Text; namespace Common
{
public class AppSettings
{
public static IConfiguration Configuration { get; set; } public static void Initial(IConfiguration configuration)
{
Configuration = configuration;
} /// <summary>
/// 封装要操作的字符
/// </summary>
/// <param name="sections"></param>
/// <returns></returns>
public static string app(params string[] sections)
{
try
{
var val = string.Empty;
for (int i = 0; i < sections.Length; i++)
{
val += sections[i] + ":";
} return Configuration[val.TrimEnd(':')];
}
catch (Exception)
{
return "";
}
}
}
}
然后我们需要在strartup里面为这个Configuration赋值
public Startup(IConfiguration configuration, IWebHostEnvironment env)
{
AppSettings.Initial(configuration);
}
在上面这个里面我看见有个老哥的写法是下面这种,但是经过我的测试,直接按照我的这种写法操作也是可以的
该图借鉴于:https://www.cnblogs.com/wangpengzong/p/12700974.html
最新文章
- Java 第7章 数组
- oracle max()函数和min()函数
- CODEVS 3000公路修建问题
- MVC ViewEngine视图引擎解读及autofac的IOC运用实践
- 程序员求职之道(《程序员面试笔试宝典》)之求职有用网站及QQ群一览表
- 【Machine Learning】Mahout基于协同过滤(CF)的用户推荐
- DDCTF-2019
- C语言博客作业06——结构体&;文件
- 几个js框架
- ubuntun 18.04 安装google浏览器
- eclipse 安装配置
- PHP中文转拼音函数
- 跨域请求中预检请求options之坑
- QL Server 高可用性(一)AlwaysOn 技术
- Brainfuck解析器(Python)
- webapp定位
- Linux学习1-Xshell连接阿里云ECS服务器
- oracle内存结构
- Markdown中的表格
- Intellij Idea 将java项目打包成jar