简体   繁体   English

如何读取appsettings.json值ASP.NET Core

[英]How to read appsettings.json values ASP.NET Core

After I've read this article about dependency injection Here I still do not have a clear understanding on how to read the appsetting in other than a controller classes. 我读过这篇文章大约依赖注入后, 在这里我仍然没有就如何读取其他的appsetting比控制器类清醒的认识。

Lets say for instance I have a helper class with a bunch of static methods that I'm planning to use, I do not create an instance of this class, how do I read setting values to use inside the methods of this class? 可以说,例如,我有一个计划使用一堆静态方法的帮助程序类,没有创建此类的实例,如何读取设置值以在此类方法中使用?

I used to create helper class to read data from appsettings.config in one of my applications: 我曾经创建助手类来从我的一个应用程序中的appsettings.config中读取数据:

public static class ConfigValueProvider
{
    private static readonly IConfigurationRoot Configuration;

    static ConfigValueProvider()
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

        Configuration = builder.Build();
    }

    public static string Get(string name)
    {
        return Configuration[name];
    }
}

However later I reviewed my application to get away from static methods which depends on application config in order to make my application testable. 但是,后来我查看了我的应用程序,以摆脱依赖于应用程序配置的static方法,以使我的应用程序可测试。

You should use services.Configure as below: 您应该使用services.Configure如下:

   public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
          services.Configure<JSonAsClass>(Configuration.GetSection("MySectionName"));
          services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    }

then you can inject JSonAsClass inside any class you want to use it: 那么您可以将JSonAsClass注入要使用的任何类中:

        private JSonAsClass jSonAsClass;
        public MailService(IOptions<JSonAsClass> jSonAsClass)
        {
            this.jSonAsClass = jSonAsClass.Value;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 github 中的 asp.net 核心 appsettings.json - asp.net core appsettings.json in github ASP.NET Core appSettings.json安全 - ASP.NET Core appSettings.json security 使用ASP.NET Core反序列化appsettings.json - Deserialize appsettings.json with ASP.NET Core Application Insights ASP.NET核心环境appsettings.json - Application Insights ASP.NET Core Environment appsettings.json 在asp.net核心的appsettings.json中 - basedir in appsettings.json in asp.net core ASP.NET核心EF代码首先appsettings.json - ASP.NET Core EF Code First appsettings.json 如何在 ASP.net Core 2 的 appsettings.json 中加密密码? - How do you encrypt a password within appsettings.json for ASP.net Core 2? 如何从.Net 6 中的 class 中的 appsettings.json 读取值? - How to read values from appsettings.json inside a class in .Net 6? 无法让ConfigurationBuilder从ASP.NET Core 1.0 RC2中的xunit类库中的appsettings.json读取 - Unable to get ConfigurationBuilder to read from appsettings.json in xunit class library in ASP.NET Core 1.0 RC2 ASP.NET Core 1.0 ConfigurationBuilder().AddJsonFile(“appsettings.json”); 找不到文件 - ASP.NET Core 1.0 ConfigurationBuilder().AddJsonFile(“appsettings.json”); not finding file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM