本文介绍了在服务.NET Core中获取appsettings.json值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有 appsettings.json
文件,我想在其中声明文件的路径。
I have appsettings.json
file where I want to declare paths to files.
路径:{
file: C:/file.pdf
}
我想在我的服务中访问此值,我尝试这样做:
I want to access this value in my service, I try it to do like this:
public class ValueService: IValueService
{
IConfiguration Configuration { get; set; }
public MapsService(IConfiguration configuration)
{
this.Configuration = configuration;
}
public string generateFile()
{
var path = Configuration["Paths:file"] ;
}
}
但是我得到 var path
Startup.cs
文件具有 appsettings.json
声明为从那里获取连接字符串。是否可以在 startup.cs
类之外访问这些值?
Startup.cs
file has appsettings.json
declared as it takes connection string from there. Is it possible to access these values outside startup.cs
class?
推荐答案
您应该在ConfigureServices中注册配置:
You should register Configuration in ConfigureServices:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddSingleton<IConfiguration>(Configuration);
}
这篇关于在服务.NET Core中获取appsettings.json值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!