无法在 asp.net mvc 6 beta 8 中检索 AppSettings
Can't retrieve AppSettings in asp.net mvc 6 beta 8
我正在尝试创建一个新的 ASP.Net MV5 项目(目前是 beta8)以供学习。
我对获取应用程序设置的简单案例感到困惑。
我在 appsettings.json 文件中添加了一些配置:
{
"Data": {
"DefaultConnection": {
"ConnectionString": "Server=(localdb)\mssqllocaldb;Database=aspnet5-someproject-e84e86e2-0fec-4132-9a91-2f6c4b4c61a3;Trusted_Connection=True;MultipleActiveResultSets=true"
}
},
"AppSettings": {
"CloudStorageContainerReference": "someproject",
"StorageConnectionString": "UseDevelopmentStorage=true"
}
}
我还创建了一个强类型 class:
public class AppSettings
{
public string CloudStorageContainerReference { get; set; }
public string StorageConnectionString { get; set; }
}
并更新了我的启动文件:
public void ConfigureServices(IServiceCollection services)
{
// Add Entity Framework services to the services container.
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
// Add Identity services to the services container.
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
// Add settings
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
// Add MVC services to the services container.
services.AddMvc();
// Register application services.
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
}
此时,如果我破解代码,我可以看到 Configuration.GetSection("AppSettings").Value
为空。
此外,我想在我的控制器中注入这些设置:
public class TransfertController : Controller
{
private IOptions<AppSettings> AppSettings;
public TransfertController(IOptions<AppSettings> appSettings)
{
if (AppSettings == null) throw new ArgumentNullException(nameof(appSettings));
AppSettings = appSettings;
}
}
但它抛出 NullReferenceException
,因为我的 appSettings
参数为空。
缺少什么?
[编辑]:作为旁注,这一行:
Configuration["AppSettings:CloudStorageContainerReference"]
Returns其实是正确的单值。
不确定为什么,但我通过将我的 class 成员 "AppSettings" 重命名为 "Settings" 并展开 IOptions 解决了这个问题:
public class TransfertController : Controller
{
private readonly AppSettings Settings;
public TransfertController(IOptions<AppSettings> settings)
{
if (settings == null) throw new ArgumentNullException(nameof(settings));
Settings = settings.Value;
}
}
正在工作...
我会说这只是您原始代码中的错字:
public TransfertController(IOptions<AppSettings> appSettings)
{
if (AppSettings == null) throw new ArgumentNullException(nameof(settings));
AppSettings = appSettings;
}
在你的守卫中,你正在检查你正在分配的相同 属性 AppSettings
,然后再分配它。您希望警卫检查方法输入参数,而不是 if (appSettings == null) throw new ...
中的 属性(注意小写 appSettings)
我正在尝试创建一个新的 ASP.Net MV5 项目(目前是 beta8)以供学习。
我对获取应用程序设置的简单案例感到困惑。
我在 appsettings.json 文件中添加了一些配置:
{
"Data": {
"DefaultConnection": {
"ConnectionString": "Server=(localdb)\mssqllocaldb;Database=aspnet5-someproject-e84e86e2-0fec-4132-9a91-2f6c4b4c61a3;Trusted_Connection=True;MultipleActiveResultSets=true"
}
},
"AppSettings": {
"CloudStorageContainerReference": "someproject",
"StorageConnectionString": "UseDevelopmentStorage=true"
}
}
我还创建了一个强类型 class:
public class AppSettings
{
public string CloudStorageContainerReference { get; set; }
public string StorageConnectionString { get; set; }
}
并更新了我的启动文件:
public void ConfigureServices(IServiceCollection services)
{
// Add Entity Framework services to the services container.
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
// Add Identity services to the services container.
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
// Add settings
services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
// Add MVC services to the services container.
services.AddMvc();
// Register application services.
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
}
此时,如果我破解代码,我可以看到 Configuration.GetSection("AppSettings").Value
为空。
此外,我想在我的控制器中注入这些设置:
public class TransfertController : Controller
{
private IOptions<AppSettings> AppSettings;
public TransfertController(IOptions<AppSettings> appSettings)
{
if (AppSettings == null) throw new ArgumentNullException(nameof(appSettings));
AppSettings = appSettings;
}
}
但它抛出 NullReferenceException
,因为我的 appSettings
参数为空。
缺少什么?
[编辑]:作为旁注,这一行:
Configuration["AppSettings:CloudStorageContainerReference"]
Returns其实是正确的单值。
不确定为什么,但我通过将我的 class 成员 "AppSettings" 重命名为 "Settings" 并展开 IOptions 解决了这个问题:
public class TransfertController : Controller
{
private readonly AppSettings Settings;
public TransfertController(IOptions<AppSettings> settings)
{
if (settings == null) throw new ArgumentNullException(nameof(settings));
Settings = settings.Value;
}
}
正在工作...
我会说这只是您原始代码中的错字:
public TransfertController(IOptions<AppSettings> appSettings)
{
if (AppSettings == null) throw new ArgumentNullException(nameof(settings));
AppSettings = appSettings;
}
在你的守卫中,你正在检查你正在分配的相同 属性 AppSettings
,然后再分配它。您希望警卫检查方法输入参数,而不是 if (appSettings == null) throw new ...
中的 属性(注意小写 appSettings)