不确定我在这里错过了什么,但我无法从我的应用程序设置中获得值。Json在我的。net核心应用程序。我有我的appsettings。json:

{
    "AppSettings": {
        "Version": "One"
    }
}

启动:

public class Startup
{
    private IConfigurationRoot _configuration;
    public Startup(IHostingEnvironment env)
    {
        _configuration = new ConfigurationBuilder()
    }
    public void ConfigureServices(IServiceCollection services)
    {
      //Here I setup to read appsettings        
      services.Configure<AppSettings>(_configuration.GetSection("AppSettings"));
    }
}

模型:

public class AppSettings
{
    public string Version{ get; set; }
}

控制器:

public class HomeController : Controller
{
    private readonly AppSettings _mySettings;

    public HomeController(IOptions<AppSettings> settings)
    {
        //This is always null
        _mySettings = settings.Value;
    }
}

_mySettings总是空的。我是不是遗漏了什么?


当前回答

我认为最好的选择是:

创建一个模型类作为配置模式 在DI注册: services.Configure (Configuration.GetSection(“democonfig”)); 从控制器中的DI中获取模型对象的值: private readonly myConfig; (IOptions<your_model> configOps) { 这一点。myConfig = configOps.Value; }

其他回答

花了一个小时试图解决同样的问题,我的解决方案是为appconfig添加PreserveNewest/CopyAlways。csproj中的Json

<None Update="appsettings.json">
  <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>

像这样在这里添加所需的键。在这种情况下,它的securecookie:

在startup.cs中添加构造函数 公共启动(IConfiguration配置) { 配置=配置; } public IConfiguration Configuration {get;} 使用Configuration[" secucookie "]访问设置

程序与启动课程

ASP。NET Core 6.x

ASP。NET Core 6。x在Program类中带来了另一个大的变化:

如果您选择使用顶级语句,则没有Program.Main()样板 隐式使用指令 没有启动类,因为所有东西都在程序文件中 介绍WebApplication和WebApplicationBuilder

有人说,这些变化有利于新人学习ASP。净的核心。我的感觉正好相反。我认为程序和启动的分离更有意义,至少对我来说。

无论如何……

这是Program.cs的样子:

// Program.cs
public class Program
{
    public static void Main(string[] args)
    {
        var builder = WebApplication.CreateBuilder(args);

        builder.Services.AddControllersWithViews();

        var app = builder.Build();

        if (!app.Environment.IsDevelopment())
        {
            app.UseExceptionHandler("/errors");
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();

        app.UseRouting();

        app.UseAuthorization();

        app.MapControllerRoute(
            name: "areaRoute",
            pattern: "{area:exists}/{controller=home}/{action=index}/{id?}");
            
        app.MapControllerRoute(
            name: "default",
            pattern: "{controller=home}/{action=index}/{id?}");

        app.Run();
    }
}

你可以区分WebApplication.CreateBuilder()和builder.Build()之间的部分是旧的ConfigureServices(IServiceCollection服务)用来做的事情。app.Run()之前的部分是Startup中旧的Configure()所做的。

最重要的是,IConfiguration被注入到管道中,所以你可以在你的控制器上使用它。

ASP。NET Core 3。X至5

ASP。NET Core 3。X带来了一些变化,试图支持其他方法,如worker services,所以它用更通用的主机构建器取代了web主机:

// Program.cs
public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            }; 
    }
}

Startup类看起来非常类似于2。不过是X版本。

ASP。NET Core 2.x

你不需要在启动构造函数中新建IConfiguration。它的实现将由DI系统注入。

// Program.cs
public class Program
{
    public static void Main(string[] args)
    {
        BuildWebHost(args).Run();
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .Build();            
}

// Startup.cs
public class Startup
{
    public IHostingEnvironment HostingEnvironment { get; private set; }
    public IConfiguration Configuration { get; private set; }

    public Startup(IConfiguration configuration, IHostingEnvironment env)
    {
        this.HostingEnvironment = env;
        this.Configuration = configuration;
    }
}

ASP。NET Core 1.x

您需要告诉Startup加载appsettings文件。

// Program.cs
public class Program
{
    public static void Main(string[] args)
    {
        var host = new WebHostBuilder()
            .UseKestrel()
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>()
            .UseApplicationInsights()
            .Build();

        host.Run();
    }
}

//Startup.cs
public class Startup
{
    public IConfigurationRoot Configuration { get; private set; }

    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
            .AddEnvironmentVariables();

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

获得值

有很多方法可以从应用程序设置中获得你配置的值:

使用ConfigurationBuilder的简单方法。GetValue < T > 使用选项模式

比如说你的appsettings。Json是这样的:

{
    "ConnectionStrings": {
        ...
    },
    "AppIdentitySettings": {
        "User": {
            "RequireUniqueEmail": true
        },
        "Password": {
            "RequiredLength": 6,
            "RequireLowercase": true,
            "RequireUppercase": true,
            "RequireDigit": true,
            "RequireNonAlphanumeric": true
        },
        "Lockout": {
            "AllowedForNewUsers": true,
            "DefaultLockoutTimeSpanInMins": 30,
            "MaxFailedAccessAttempts": 5
        }
    },
    "Recaptcha": { 
        ...
    },
    ...
}

简单的方法

你可以将整个配置注入到你的控制器/类的构造函数中(通过IConfiguration),并通过指定的键获取你想要的值:

public class AccountController : Controller
{
    private readonly IConfiguration _config;

    public AccountController(IConfiguration config)
    {
        _config = config;
    }

    [AllowAnonymous]
    public IActionResult ResetPassword(int userId, string code)
    {
        var vm = new ResetPasswordViewModel
        {
            PasswordRequiredLength = _config.GetValue<int>(
                "AppIdentitySettings:Password:RequiredLength"),
            RequireUppercase = _config.GetValue<bool>(
                "AppIdentitySettings:Password:RequireUppercase")
        };

        return View(vm);
    }
}

选择模式

ConfigurationBuilder。如果你只需要应用程序设置中的一个或两个值,那么>工作得很好。但如果你想从应用程序设置中获得多个值,或者你不想在多个地方硬编码那些关键字字符串,使用选项模式可能更容易。选项模式使用类来表示层次结构/结构。

使用选项模式:

定义类来表示结构 注册这些类绑定到的配置实例 将IOptions<T>注入到你想获取值的控制器/类的构造函数中

1. 定义配置类来表示结构

你可以定义具有属性的类,这些属性需要与应用程序设置中的键完全匹配。类的名称不必与应用设置中的section名称匹配:

public class AppIdentitySettings
{
    public UserSettings User { get; set; }
    public PasswordSettings Password { get; set; }
    public LockoutSettings Lockout { get; set; }
}

public class UserSettings
{
    public bool RequireUniqueEmail { get; set; }
}

public class PasswordSettings
{
    public int RequiredLength { get; set; }
    public bool RequireLowercase { get; set; }
    public bool RequireUppercase { get; set; }
    public bool RequireDigit { get; set; }
    public bool RequireNonAlphanumeric { get; set; }
}

public class LockoutSettings
{
    public bool AllowedForNewUsers { get; set; }
    public int DefaultLockoutTimeSpanInMins { get; set; }
    public int MaxFailedAccessAttempts { get; set; }
}

2. 注册配置实例

然后你需要在启动时在ConfigureServices()中注册这个配置实例:

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
...

namespace DL.SO.UI.Web
{
    public class Startup
    {
        ...
        public void ConfigureServices(IServiceCollection services)
        {
            ...
            var identitySettingsSection = 
                _configuration.GetSection("AppIdentitySettings");
            services.Configure<AppIdentitySettings>(identitySettingsSection);
            ...
        }
        ...
    }
}

ASP。NET Core 6.x

由于我在开头提到的ASP。NET Core 6。x,你需要绑定该section并将其添加到DI,如下所示:

// Program.cs
public class Program
{
    public static void Main(string[] args)
    {
        var builder = WebApplication.CreateBuilder(args);

        builder.Services.AddControllersWithViews();

        builder.Services.Configure<AppIdentitySettings>(
            builder.Configuration.GetSection("AppIdentitySettings")
        );

        var app = builder.Build();

        ...

        app.Run();
    }
}

你可以在这里阅读更多相关内容。

3.注入IOptions

最后在你想要获取值的控制器/类上,你需要通过构造函数注入IOptions<AppIdentitySettings>:

public class AccountController : Controller
{
    private readonly AppIdentitySettings _appIdentitySettings;

    public AccountController(IOptions<AppIdentitySettings> appIdentitySettingsAccessor)
    {
        _appIdentitySettings = appIdentitySettingsAccessor.Value;
    }

    [AllowAnonymous]
    public IActionResult ResetPassword(int userId, string code)
    {
        var vm = new ResetPasswordViewModel
        {
            PasswordRequiredLength = _appIdentitySettings.Password.RequiredLength,
            RequireUppercase = _appIdentitySettings.Password.RequireUppercase
        };

        return View(vm);
    }
}

我认为最好的选择是:

创建一个模型类作为配置模式 在DI注册: services.Configure (Configuration.GetSection(“democonfig”)); 从控制器中的DI中获取模型对象的值: private readonly myConfig; (IOptions<your_model> configOps) { 这一点。myConfig = configOps.Value; }

我发现用。net Core 3+最容易做到以下几点。我发现所有使用HostBuilders等的其他方法都有点啰嗦,而且可读性不强。这不是专门针对ASP的。但是你可以调整它…

这里有一个工作示例:https://github.com/NotoriousPyro/PyroNexusTradingAlertBot/blob/develop/PyroNexusTradingAlertBot/Program.cs

创建json:

{
"GlobalConfig": {
    "BlacklistedPairs": [ "USD", "USDT", "BUSD", "TUSD", "USDC", "DAI", "USDK" ]
},
"CoinTrackingConfig": {
    "Cookie1": "",
    "Cookie2": "",
    "ApiKey": "",
    "ApiSecret": "",
    "UpdateJobs": [
    {
        "Name": "Binance",
        "Path": "binance_api",
        "JobId": 42202
    },
    {
        "Name": "Bitfinex",
        "Path": "bitfinex_api",
        "JobId": 9708
    }
    ]
},
"DiscordConfig": {
    "BotToken": ""
}
}

创建json对象的类:

class GlobalConfig
{
    public string[] BlacklistedPairs { get; set; }
}
class CoinTrackingConfig
{
    public string Cookie1 { get; set; }
    public string Cookie2 { get; set; }
    public string ApiKey { get; set; }
    public string ApiSecret { get; set; }
    public List<CoinTrackingUpdateJobs> UpdateJobs { get; set; }
}

class CoinTrackingUpdateJobs
{
    public string Name { get; set; }
    public string Path { get; set; }
    public int JobId { get; set; }
}

class DiscordConfig
{
    public string BotToken { get; set; }
}

创建一个helper类:

private class Config
{
    private IConfigurationRoot _configuration;
    public Config(string config) => _configuration = new ConfigurationBuilder()
            .AddJsonFile(config)
            .Build();

    public T Get<T>() where T : new()
    {
        var obj = new T();
        _configuration.GetSection(typeof(T).Name).Bind(obj);
        return obj;
    }
}

服务提供者选项和服务构造函数:

public class DiscordServiceOptions
{
    public string BotToken { get; set; }
}

public DiscordService(IOptions<DiscordServiceOptions> options, ILogger<DiscordService> logger)
{
    _logger = logger;
    _client = new DiscordSocketClient();
    _client.Log += Log;
    _client.Ready += OnReady;
    _client.Disconnected += OnDisconnected;
    _client.LoginAsync(TokenType.Bot, options.Value.BotToken);
    _client.StartAsync();
}

像这样初始化它(将配置传递给服务提供者- IOptions将在服务构建时传递进来):

static async Task Main(string[] args)
{
    var _config = new Config("config.json");

    var globalConfig = config.Get<GlobalConfig>();
    var coinTrackingConfig = config.Get<CoinTrackingConfig>();
    var discordConfig = config.Get<DiscordConfig>();

    _services = new ServiceCollection()
        .AddOptions()
        .Configure<DiscordServiceOptions>(options =>
        {
            options.BotToken = discordConfig.BotToken;
        })
        .AddSingleton<IDiscordService, DiscordService>()
        .AddLogging(logging =>
        {
            logging.SetMinimumLevel(LogLevel.Trace);
            logging.AddNLog(new NLogProviderOptions
            {
                CaptureMessageTemplates = true,
                CaptureMessageProperties = true
            });
        })
        .BuildServiceProvider();
}