如何从appsettings.json获取价值 [英] how to get value from appsettings.json

查看:554
本文介绍了如何从appsettings.json获取价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class Bar
{
    public static readonly string Foo = ConfigurationManager.AppSettings["Foo"];
}

在.NET Framework 4.x中,我可以使用 ConfigurationManager.AppSettings [ Foo] Webconfig 中获取 Foo ,然后我可以通过 Bar.Foo

In the .NET Framework 4.x, I can use the ConfigurationManager.AppSettings ["Foo"] to get Foo in Webconfig,and then I can easily get the value of Foo through Bar.Foo

但是在.Net核心中,我必须注入选项,并且无法获取 Foo 的值通过 Bar.Foo

But in .Net core, I mustto inject options, And can not get the value of Foothrough Bar.Foo

是否有一种方法,可以直接通过 Bar .Foo 来获取 Foo 的值?

Is there a method, which can be directly through the Bar.Foo to get the value of Foo?

推荐答案

因此实际上有两种方法可以解决此问题。

So there are really two ways to go about this.

您有一个appsettings.json文件:

You have an appsettings.json file :

{
  "myConfiguration": {
    "myProperty": true 
  }
}

您可以像这样创建配置POCO:

You create a Configuration POCO like so :

public class MyConfiguration
{
    public bool MyProperty { get; set; }
}

在startup.cs中,您的ConfigureServices中有一些用于注册配置的文件:

In your startup.cs you have something in your ConfigureServices that registers the configuration :

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<MyConfiguration>(Configuration.GetSection("myConfiguration"));
}

然后在控制器/服务中注入IOptions即可使用。

Then in your controller/service you inject in the IOptions and it's useable.

public class ValuesController : Controller
{
    private readonly MyConfiguration _myConfiguration;

    public ValuesController(IOptions<MyConfiguration> myConfiguration)
    {
        _myConfiguration = myConfiguration.Value;
    }
}

我个人不喜欢使用IOptions,因为我认为它会拖走一些我本不想要的垃圾,但是您可以做一些很酷的事情,例如热插拔和使用它的东西。

Personally I don't like using IOptions because I think it drags along some extra junk that I don't really want, but you can do cool things like hot swapping and stuff with it.

基本上是相同的,但是在您的Configure Services方法中,您绑定到了您的POCO的单例。

It's mostly the same but in your Configure Services method you instead bind to a singleton of your POCO.

public void ConfigureServices(IServiceCollection services)
{
    //services.Configure<MyConfiguration>(Configuration.GetSection("myConfiguration"));
    services.AddSingleton(Configuration.GetSection("myConfiguration").Get<MyConfiguration>());
}

然后您可以直接注入POCO:

And then you can just inject the POCO directly :

public class ValuesController : Controller
{
    private readonly MyConfiguration _myConfiguration;

    public ValuesController(MyConfiguration myConfiguration)
    {
        _myConfiguration = myConfiguration;
    }
}

有点简单,因为您可能应该使用接口来使单元测试更容易一些,但是您可以理解。

A little simplistic because you should probably use an interface to make unit testing a bit easier but you get the idea.

通常从此处获取: http://dotnetcoretutorials.com/2016/12/26/custom-configuration-sections-asp-net-core/

这篇关于如何从appsettings.json获取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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