在.NET Framework 中,配置文件通常采用XML 格式。在.NET Core中,我们建议使用特殊的ConfigurationManager来读取配置文件的内容。你想读取json文件吗?
1.读取Startup类的json配置文件
添加启动配置
varconfigurationBuilder=new ConfigurationBuilder(); var builder=configurationBuilder.AddJsonFile(\\\'UploadFile/GetCourts.json\\\')
, 错误
, 真相
).AddJsonFile(\\\'UploadFile/Queryaredatalevelall.json\\\'
, 错误
, 真相
); var 法院列表=新法院列表();
).Bind(Courtlist); services.AddSingleton(Courtlist); 读取构造函数注入
私有只读
Courtlist _Courtlist 私有只读;
AreaRootobjectlist _AreaRootobjectlist; public DmsCacheService( AreaRootobjectlist AreaRootobjectlist, Courtlist Courtlist, ILogger 记录器, AccessAddressDto accessAddress, IJudgeBannedWordsService JudgeBannedWordsService, IJudgeService JudgeService, IMapper 映射器) { this.provider=提供者; this.judgeBannedWordsService;=JudgeB annedWordsService;=映射器; this.accessAddress=accessAddress; this.logger=外套列表_AreaRootobjectlist;
() {返回
_Courtlist.Courtlists }2. 直接从硬盘读取。
定义读取方法
私有T GetDataByLocal(string jsonfileName) { var res=this.provider.Get(jsonfileName);if
(res.HasValue) {返回
res.Value; } var 文件路径=$\\\'/UploadFile/\\\'
; 字符串applicationRoot=AppContext.BaseDirectory; var Provider=new PhysicalFileProvider(applicationRoot);
(!System.IO.File.Exists(AppContext.BaseDirectory +completeFilePath)) { throw new Exception(\\\'文件不存在\\\'
); } var formFile=Provider.GetFileInfo(completeFilePath); var stream=formFile.RegisterProvider(CodePagesEncodingProvider.Instance);
); var reader=new StreamReader(stream, 编码); var result=JsonConvert.DeserializeObject(rs);
传递要读取的文件名。
公共列表GetCaseCodes
() {返回
this.GetDataByLocal(\\\'GetCaseCodes.json\\\'
);