C# 从类库项目访问appsettings.json文件设置
我知道如何在web项目上设置C# 从类库项目访问appsettings.json文件设置,c#,asp.net-core,asp.net-core-mvc,C#,Asp.net Core,Asp.net Core Mvc,我知道如何在web项目上设置appsettings.json文件。但是你能告诉我如何从类库项目中访问它吗 我可以在web项目上按如下所示配置它。那么如何从类库项目访问该值?可能吗 public class MyConfig { public string ApplicationName { get; set; } public int Version { get; set; } } public class Startup { public IConfiguratio
appsettings.json
文件。但是你能告诉我如何从类库项目中访问它吗
我可以在web项目上按如下所示配置它。那么如何从类库项目访问该值?可能吗
public class MyConfig
{
public string ApplicationName { get; set; }
public int Version { get; set; }
}
public class Startup
{
public IConfigurationRoot Configuration { get; set; }
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
Configuration = builder.Build();
}
}
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
// Add functionality to inject IOptions<T>
services.AddOptions();
// Add our Config object so it can be injected
services.Configure<MyConfig>(Configuration);
}
public class HomeController : Controller
{
private readonly IOptions<MyConfig> config;
public HomeController(IOptions<MyConfig> config)
{
this.config = config;
}
// GET: /<controller>/
public IActionResult Index() => View(config.Value);
}
公共类MyConfig
{
公共字符串ApplicationName{get;set;}
公共int版本{get;set;}
}
公营创业
{
公共IConfigurationRoot配置{get;set;}
公共启动(IHostingEnvironment环境)
{
var builder=new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile(“appsettings.json”,可选:true,重载更改:true)
Configuration=builder.Build();
}
}
public void配置服务(IServiceCollection服务)
{
services.AddMvc();
//添加功能以注入IOptions
services.AddOptions();
//添加我们的配置对象,以便可以注入它
服务。配置(配置);
}
公共类HomeController:控制器
{
私有只读配置;
公共家庭控制器(IOptions配置)
{
this.config=config;
}
//获取://
public IActionResult Index()=>视图(config.Value);
}
您需要获取节,然后将其绑定,更改此选项
// Add our Config object so it can be injected
services.Configure<MyConfig>(Configuration);
appsettings.json
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
},
"MyConfig": {
"ApplicationName": "My Application Name",
"Version": 1
}
}
我们如何称呼这个“另一个”web项目?
Asp.Net核心DI在创建控制器之前解决所有依赖项。因此,我们可以创建一个接口并实现它。然后我们可以将它注入控制器,并从那里访问appsettings.json
设置
ictegory.cs
public interface ICategory
{
IOptions<MyConfig> config;
IQueryable<Categories> Get();
}
public class Category : ICategory
{
private readonly IOptions<MyConfig> config;
public Category(IOptions<MyConfig> config)
{
this.config = config;
}
public Categories Get(long id)
{
var value = config.Value.ApplicationName;
var vars = config.Value.Version;
return category;
}
}
public CategoriesController(ICategory category)
{
this.category = category;
}
调用category.Get()
方法时,我们可以访问该方法中的appsettings.json
设置
在ConfigureServices
method ofStartup.cs
services.AddTransient<ICategory, Category>();
services.AddTransient();
您能告诉我如何将其插入到另一个项目的类文件中吗?是的,我们可以在web项目中这样做,如您所示。但是我如何在另一个类库项目中这样做呢?在同一个解决方案下,我有多个类库项目。我添加了一个示例,请检查。@Ahmar…IOptions config;,在ICategory接口中--error…接口不能包含fields@Ahmar... 该接口在我的类库项目中,DatabaseSettings类也在该项目中。我尝试添加一个图像和正确格式的代码,但在注释中找不到该方法。。。公共接口IContextSettings{IOptions AppConfig;}从AppConfig下的红色Swiggly获取“接口不能包含字段”错误消息
public CategoriesController(ICategory category)
{
this.category = category;
}
services.AddTransient<ICategory, Category>();