从类库项目访问appsettings.json文件设置 [英] Access appsettings.json file setting from the class library project

查看:274
本文介绍了从类库项目访问appsettings.json文件设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何在Web项目上设置appsettings.json文件.但是,您能告诉我如何从类库项目中访问该文件吗?

I know how to set appsettings.json file on web project.But can you tell me how to access that from the class library project ?

我可以按如下所示在Web项目上对其进行配置.然后如何从类库项目中访问该值?有可能吗?

I can configure it as shown below on the web project.Then how can I access that values from the class library project ? Is that possible ?

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);
}

推荐答案

您需要获取此部分,然后将其绑定,更改

You need to get the section and then bind it, change this

// Add our Config object so it can be injected
services.Configure<MyConfig>(Configuration);

TO

// Add our Config object so it can be injected
services.Configure<MyConfig>(option => Configuration.GetSection("MyConfig").Bind(option));

然后,您将在HomeController或您将其注入的任何其他位置获得值.

Then you will get values in HomeController or any other place where you inject this.

public class HomeController : Controller
{

    private readonly IOptions<MyConfig> config;

    public HomeController(IOptions<MyConfig> config)
    {
        this.config = config;
    }

    public IActionResult Index()
    {
        var value = config.Value.ApplicationName;
        var vars = config.Value.Version;
        return View();
    }
}

appsettings.json

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  },
  "MyConfig": {
    "ApplicationName": "My Application Name",
    "Version": 1
  }
}

我们如何称呼这是另一个Web项目?

Asp.Net Core DI在创建控制器之前先解决所有依赖项.因此,我们可以创建一个接口并实现它.然后,我们可以将其注入到控制器中,然后从其中访问appsettings.json设置.

Asp.Net Core DI resolve all dependencies before creating controller. So,we can create an interface and implement it. Then We can inject it into controller and from there access the appsettings.json settings.

ICategory.cs

public interface ICategory
{
    IOptions<MyConfig> config;
    IQueryable<Categories> Get();
}

Category.cs

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;
      }
}

CategoryController.cs

public CategoriesController(ICategory category)
{
   this.category = category;
}

当我们调用category.Get()方法时,可以访问该方法中的appsettings.json设置.

When we call category.Get() method we can access the appsettings.json settings in that method.

Startup.csConfigureServices方法中注册

services.AddTransient<ICategory, Category>();

这篇关于从类库项目访问appsettings.json文件设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆