【问题标题】:Moving Cache Profiles Settings to appsettings.json in ASP.NET Core将缓存配置文件设置移动到 ASP.NET Core 中的 appsettings.json
【发布时间】:2015-08-07 09:53:51
【问题描述】:

我希望能够在我的 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",
    }
  }
}

但是,我无法让它工作。尝试绑定配置部分时,我不断收到反射错误。

【问题讨论】:

标签: asp.net-core asp.net-core-mvc


【解决方案1】:

例子:

public class CacheProfileSettings
{
    public Dictionary<string, CacheProfile> CacheProfiles { get; set; }
}

//------------------------------------------

var cacheProfileSettings = ConfigurationBinder.Bind<CacheProfileSettings>(config.GetConfigurationSection("CacheProfiles"));
var cacheProfile = cacheProfileSettings.CacheProfiles["RobotsText"];

【讨论】:

  • 所以我的字典需要一个包装器并且不能直接绑定到字典?
猜你喜欢
  • 1970-01-01
  • 2017-12-15
  • 1970-01-01
  • 2018-02-13
  • 2020-06-05
  • 2010-10-23
  • 1970-01-01
  • 2017-04-30
  • 1970-01-01
相关资源
最近更新 更多