从类库项目中访问appsettings.json文件设置

时间:2017-01-17 11:25:05

标签: 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 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);
}

1 个答案:

答案 0 :(得分:3)

您需要获取该部分然后绑定它,更改此

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

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

然后,您将获得HomeController或您注入此内容的任何其他地方的值。

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

enter image description here

我们如何称呼其他网络项目?

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

<强> 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设置。

ConfigureServices

Startup.cs方法注册
services.AddTransient<ICategory, Category>();