将缓存配置文件设置移动到 ASP.NET 核心中的 appsettings.json
Moving Cache Profiles Settings to appsettings.json in ASP.NET Core
我希望能够在我的 config.json 文件中编辑缓存配置文件设置。您可以在 ASP.NET 4.6 中使用 web.config 文件做类似的事情。这就是我现在拥有的:
services.ConfigureMvc(
mvcOptions =>
{
mvcOptions.CacheProfiles.Add(
"RobotsText",
new CacheProfile()
{
Duration = 86400,
Location = ResponseCacheLocation.Any,
// VaryByParam = "none" // Does not exist in MVC 6 yet.
});
});
我想做这样的事情:
services.ConfigureMvc(
mvcOptions =>
{
var cacheProfiles = ConfigurationBinder.Bind<Dictionary<string, CacheProfile>>(
Configuration.GetConfigurationSection("CacheProfiles"));
foreach (var keyValuePair in cacheProfiles)
{
mvcOptions.CacheProfiles.Add(keyValuePair);
}
});
我的 appsettings.json
文件如下所示:
{
"AppSettings": {
"SiteTitle": "ASP.NET MVC Boilerplate"
}
"CacheProfiles" : {
"RobotsText" : {
"Duration" : 86400,
"Location" : "Any",
}
}
}
但是,我无法让它工作。我在尝试绑定配置部分时不断收到反射错误。
示例:
public class CacheProfileSettings
{
public Dictionary<string, CacheProfile> CacheProfiles { get; set; }
}
//------------------------------------------
var cacheProfileSettings = ConfigurationBinder.Bind<CacheProfileSettings>(config.GetConfigurationSection("CacheProfiles"));
var cacheProfile = cacheProfileSettings.CacheProfiles["RobotsText"];
我希望能够在我的 config.json 文件中编辑缓存配置文件设置。您可以在 ASP.NET 4.6 中使用 web.config 文件做类似的事情。这就是我现在拥有的:
services.ConfigureMvc(
mvcOptions =>
{
mvcOptions.CacheProfiles.Add(
"RobotsText",
new CacheProfile()
{
Duration = 86400,
Location = ResponseCacheLocation.Any,
// VaryByParam = "none" // Does not exist in MVC 6 yet.
});
});
我想做这样的事情:
services.ConfigureMvc(
mvcOptions =>
{
var cacheProfiles = ConfigurationBinder.Bind<Dictionary<string, CacheProfile>>(
Configuration.GetConfigurationSection("CacheProfiles"));
foreach (var keyValuePair in cacheProfiles)
{
mvcOptions.CacheProfiles.Add(keyValuePair);
}
});
我的 appsettings.json
文件如下所示:
{
"AppSettings": {
"SiteTitle": "ASP.NET MVC Boilerplate"
}
"CacheProfiles" : {
"RobotsText" : {
"Duration" : 86400,
"Location" : "Any",
}
}
}
但是,我无法让它工作。我在尝试绑定配置部分时不断收到反射错误。
示例:
public class CacheProfileSettings
{
public Dictionary<string, CacheProfile> CacheProfiles { get; set; }
}
//------------------------------------------
var cacheProfileSettings = ConfigurationBinder.Bind<CacheProfileSettings>(config.GetConfigurationSection("CacheProfiles"));
var cacheProfile = cacheProfileSettings.CacheProfiles["RobotsText"];