将缓存配置文件设置移动到ASP

将缓存配置文件设置移动到ASP

本文介绍了将缓存配置文件设置移动到ASP.NET Core中的appsettings.json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在config.json文件中编辑缓存配置文件设置.您可以使用web.config文件在ASP.NET 4.6中执行类似的操作.这就是我现在拥有的:

I would like to be able to edit the cache profile settings in my config.json file. You could do something similar in ASP.NET 4.6 with the web.config file. This is what I have now:

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",
    }
  }
}

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

However, I have not been able to get this to work. I keep getting reflection errors upon trying to bind the configuration section.

推荐答案

示例:

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

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

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

这篇关于将缓存配置文件设置移动到ASP.NET Core中的appsettings.json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 23:56