asp.net core 取消了web.config配置文件,而将appsetting.json作为了配置文件。

那么,怎么读取相关数据呢?这里我在appsetting.json中添加一些信息

第一种:在.net core 类库中读取json文件

{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"Appsettings": {
"ConnectionString": "Persist Security Info=True;Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.17.202)(PORT=1032)))(CONNECT_DATA=(SERVICE_NAME = orcl)));User Id=ircs;Password=123456",
"Port": ,
"ReportDir": "C:\\Team2020\\ICRS\\ReportDir", //上报文件存放目录(未加密)
"entityFilePath": "D:\\test", //监测日志实体文件存放目录
"ircsId": "AB123", //经营者ID
"level": "" //违法违网站处置规指令等级
},
"AllowedHosts": "*"
}

这里我们需要添加netGet包:

注意:与读取Web.Config不同的是,asp.net 添加引用 using System.Configuration;

asp.net core 需要添加三个程序包,分别为:

Microsoft.Extensions.Configuration;
Microsoft.Extensions.Configuration.Abstractions;
Microsoft.Extensions.Configuration.Json;

加载配置文件的服务类:

    public class AppConfigurtaionServices
{
public static IConfiguration Configuration { get; set; }
static AppConfigurtaionServices()
{
//ReloadOnChange = true 当appsettings.json被修改时重新加载
Configuration = new ConfigurationBuilder()
//.SetBasePath(Directory.GetCurrentDirectory())
//AppDomain.CurrentDomain.BaseDirectory是程序集基目录,所以appsettings.json,需要复制一份放在程序集目录下,
.SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
.Add(new JsonConfigurationSource { Path = "appsettings.json", ReloadOnChange = true })
.Build();
} }

获取数据:

var conStr = AppConfigurtaionServices.Configuration.GetSection("Appsettings:ConnectionString").Value;
var conStr2 = AppConfigurtaionServices.Configuration.GetSection("Appsettings").GetSection("ConnectionString").Value;

获取oracle数据库连接信息(两种写法都行):

第二种,控制器中获取json,(采用依赖注入的方式,注入获取配置文件的服务)

首先新建一个类,名称随意如:AppSettingModel

    /// <summary>
/// 配置文件实体类
/// </summary>
public class AppSettingModel
{
public string ConnectionString { get; set; }
public string Port { get; set; }
public string ReportDir { get; set; }
public string EntityFilePath { get; set; }
public string IrcsId { get; set; }
public string Level { get; set; }
}

注意字段名要与配置文件的name一致。

在StartUp类的ConfigureServices 中加上:

//则是自动初始化AppSettings实例并且映射appSettings里的配置
services.Configure<AppSettingModel>(Configuration.GetSection("Appsettings"));

如果;IServiceCollection找不到,需要在NewGet添加   Microsoft.Extensions.DependencyInjection;

控制器获取相关数据:

public class AppSettingInfoController : Controller
{
public readonly AppSettingModel _appSettingModel;
//IOptions找不到,需要NuGet添加包 Microsoft.Extensions.Options;
public AppSettingInfoController(IOptions<AppSettingModel> appSettingModel)
{
_appSettingModel = appSettingModel.Value;
} public ActionResult Index()
{
ViewData["conStr"] = _appSettingModel.ConnectionString;
return null;
}

最新文章

  1. Css3新特性应用之视觉效果
  2. JAVA集合类型详解
  3. LeetCode Read N Characters Given Read4 II - Call multiple times
  4. js基础之事件
  5. 不使用var定义变量和使用var的区别
  6. UVA - 572 Oil Deposits(dfs)
  7. Android再学习-20141022-Activity的生命周期
  8. android入门——Activity(2)
  9. Python之路Day3
  10. Android studio启动后卡在refreshing gradle project(包解决)
  11. 求导程序编写(oo-java编程)
  12. tcp的粘包现象与解决方案
  13. 算法(第四版)C# 习题题解——1.2
  14. [Object Tracking] Contour Detection through Tensorflow running on smartphone
  15. [Spark RDD_add_1] groupByKey &amp; reduceBykey 的区别
  16. JavaScript:Functions
  17. C链表的简单案例
  18. Oracle性能诊断艺术-读书笔记(范围分区)
  19. Sailing
  20. python执行时遇到 KeyError: b&#39;somevar&#39; 时需要想到的

热门文章

  1. django framework插件使用1
  2. Spring Boot Admin 详解(Spring Boot 2.0,基于 Eureka 的实现)
  3. 第12节-BLE协议HCI层的数据格式
  4. flask实战-个人博客-表单
  5. Consul 知识点
  6. UiPath: Send SMTP Mail Message 发送带附件的邮件
  7. Jmeter做webservices接口测试
  8. java 的守护进程脚本
  9. 关于System.InvalidOperationException异常
  10. ABP abp zreo 老版本 支持dotnet framework 4.0