当前位置:  开发笔记 > 编程语言 > 正文

asp.net 5(mvc6配置)无法从appsettings.json读取

如何解决《asp.net5(mvc6配置)无法从appsettings.json读取》经验,为你挑选了1个好方法。

appsettings.json

{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
  "Default": "Verbose",
  "System": "Information",
  "Microsoft": "Information"
},
"CustomSettings": {
  "UseDataCaching": true
  }
 }
}

选项类

public class CustomSettings
{
    public bool UseDataCaching { get; set; }
}

startup.cs

 public void ConfigureServices(IServiceCollection services)
    {
        ...
        services.AddOptions();
        services.Configure(Configuration);
        ...
    }

    public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
    {
        // Set up configuration sources.

        var builder = new ConfigurationBuilder()
            .SetBasePath(appEnv.ApplicationBasePath)
            .AddJsonFile("appsettings.json")
            .AddEnvironmentVariables()
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

        if (env.IsDevelopment())
        {
            // For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709
            builder.AddUserSecrets();

            // This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately.
            builder.AddApplicationInsightsSettings(developerMode: true);
        }

        builder.AddEnvironmentVariables();
        Configuration = builder.Build();
    }

调节器

 protected IMemoryCache MemoryCache { get; }
    protected IOptions CustomSettings { get; set; }
    public HomeController(IMemoryCache cache, IOptions customSettings)
    {

        MemoryCache = cache;
        CustomSettings = customSettings;
    }

customSettings.Value.UseDataCaching始终为false,即使在appsettings.json中我将其设置为true.

不确定我是否错过了非常明显的东西

编辑:添加启动构造函数

用法:

     public IActionResult About()
    {
        var isUsingDataCache = CustomSettings.Value.UseDataCaching;
        ViewData["Message"] = "Your application description page.";

        return View();
    }

编辑:改变启动采取2参数

示例项目不起作用 http://www.megafileupload.com/a2hn/WebApplication1.zip



1> msmolcic..:

不确定如何创建Configuration对象,但要确保appsettings.json在其管道中添加文件,也BasePath为您的应用程序添加.例如:

public static IConfigurationRoot Configuration;

// You can use both IHostingEnvironment and IApplicationEnvironment at the same time.
// Instances of them are being injected at runtime by dependency injection.
public Startup(IHostingEnvironment hostingEnv, IApplicationEnvironment appEnv)
{
    // Set up configuration sources.
    var builder = new ConfigurationBuilder()
        .SetBasePath(appEnv.ApplicationBasePath)
        .AddJsonFile("appsettings.json")
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);

    if (env.IsDevelopment())
    {
        // For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709
        builder.AddUserSecrets();

        // This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately.
        builder.AddApplicationInsightsSettings(developerMode: true);
    }

    builder.AddEnvironmentVariables();
    Configuration = builder.Build();
}

编辑:

尝试更改ConfigureServices方法:

services.Configure(Configuration); // from this
services.Configure(Configuration.GetSection("CustomSettings")); // to this

编辑2:

你可以尝试在控制器的构造函数中创建你的对象,而不是IOptions像这样的泛型类型:

private CustomSettings _customSettings;

public YourControllerName(CustomSettings customSettings)
{
    _customSettings = customSettings;
}

Startup.cs ConfigureServices方法中,设置:

services.AddSingleton();

然后称之为:

public IActionResult About()
{
    var isUsingDataCache = _customSettings.UseDataCaching;
    ViewData["Message"] = "Your application description page.";

    return View();
}

编辑3:

CustomSettings的json Logging参数放在参数内.尝试将您的json更改为:

{
    "Logging": {
        "IncludeScopes": false,
        "LogLevel": {
            "Default": "Verbose",
            "System": "Information",
            "Microsoft": "Information"
        }
    },
    "CustomSettings": {
        "UseDataCaching": true
    }
}

编辑4:

我实际上试过这个.创建了新的新项目,用Edit 3中的appsettings.json数据替换了默认文件.在我放置的第一行方法里面:Startup.csConfigureServices

public void ConfigureServices(IServiceCollection services)
{
    services.Configure(Configuration.GetSection("CustomSettings"));

    ...
}

最后,我在里面测试了它HomeController:

例

推荐阅读
云聪京初瑞子_617
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有