不确定我在这里错过了什么,但我无法从我的应用程序设置中获得值。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总是空的。我是不是遗漏了什么?
这里是一个关于。net框架和Core: web的抽象。Config, app.config和appsettings.json
static SafeDictionary<string, string> _appSettings;
public static SafeDictionary<string, string> AppSettings {
get {
if (_appSettings == null)
{
_appSettings = ConfigurationManager.AppSettings
.ToDictionary()
.ToSafe();
BuildAppSettings( JsonAppSettings, "");
}
return _appSettings;
}
}
static SafeDictionary<string, string> _connectionStrings;
public static SafeDictionary<string, string> ConnectionStrings
{
get
{
if (_connectionStrings == null)
{
_connectionStrings = ConfigurationManager.ConnectionStrings
.Cast<ConnectionStringSettings>()
.ToDictionary(x => x.Name, x => x.ConnectionString)
.ToSafe();
foreach (var jp in JsonAppSettings["ConnectionStrings"].Cast<JProperty>())
_connectionStrings.Add(jp.Name, jp.Value.ToString() );
}
return _connectionStrings;
}
}
https://github.com/bitministry/common
再加上David Liang对Core 2.0的回答——
appsettings。json文件链接到ASPNETCORE_ENVIRONMENT变量。
ASPNETCORE_ENVIRONMENT可以设置为任何值,但是框架支持三个值:Development、Staging和Production。如果没有设置ASPNETCORE_ENVIRONMENT,它将默认为Production。
对于这三个值,这些appsettings.ASPNETCORE_ENVIRONMENT。json文件支持开箱即用- appsettings.Staging。appsettings.Development.json和appsettings.Production.json
以上三个应用程序设置json文件可用于配置多个环境。
示例- appsettings.Staging.json
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"System": "Information",
"Microsoft": "Information"
}
},
"MyConfig": "My Config Value for staging."
}
使用Configuration["config_var"]检索任何配置值。
public class Startup
{
public Startup(IHostingEnvironment env, IConfiguration config)
{
Environment = env;
Configuration = config;
var myconfig = Configuration["MyConfig"];
}
public IConfiguration Configuration { get; }
public IHostingEnvironment Environment { get; }
}
亚比谢克的回答是:
如果你想将值导入到静态类中,那么只需使用(ReSharper推荐):
static IConfiguration conf = (JsonConfigurationExtensions.AddJsonFile(new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory()), "appsettings.json").Build());
private static string AuthorizationServiceURL { get; } = conf["ServiceUrls:AuthorizationUrl"];
// appsettings.json
{
"ServiceUrls": {
"AuthorizationUrl": "https://example.com/authorize"
}
}
这通常发生在vscode中,因为配置launch.json的方式不同。
基于这个答案,我不得不重新配置基本路径,配置正在搜索的DLL的路径,因为默认设置是可选的,很难在.net core 3.1和net 5.0应用程序中追踪到这一点。以下是我如何重新配置的
Program.cs:
using System;
using System.IO;
using System.Reflection;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
namespace API
{
public class Program
{
public static int Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
return 0;
}
public static IHostBuilder CreateHostBuilder(string[] args)
{
return Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration(c =>
{
var codeBase = Assembly.GetExecutingAssembly().Location;
var uri = new UriBuilder(codeBase);
var path = Uri.UnescapeDataString(uri.Path);
var assembyDirectory = Path.GetDirectoryName(path);
c.SetBasePath(assembyDirectory);
})
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
})
;
}
}
}
我可以在Startup.cs中访问配置:
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Model;
namespace API
{
public class Startup
{
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
var myOptions = Configuration.To<ApiConfig>();
services.AddAuthentication(myOptions.Secret);
services.AddControllers();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
}
}
}