0

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.
            });
    });

I would like to do something like this:

services.ConfigureMvc(
    mvcOptions =>
    {
        var cacheProfiles = ConfigurationBinder.Bind<Dictionary<string, CacheProfile>>(
            Configuration.GetConfigurationSection("CacheProfiles"));
        foreach (var keyValuePair in cacheProfiles)
        {
            mvcOptions.CacheProfiles.Add(keyValuePair);
        }
    });

With my appsettings.json file looking like this:

{
  "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.

Muhammad Rehan Saeed
  • 35,627
  • 39
  • 202
  • 311
  • 1
    Take a look at the tests using this: https://github.com/aspnet/Configuration/blob/dev/test/Microsoft.Framework.Configuration.Binder.Test/ConfigurationCollectionBindingTests.cs#L358 – Kiran Aug 07 '15 at 15:40

1 Answers1

3

Example:

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

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

var cacheProfileSettings = ConfigurationBinder.Bind<CacheProfileSettings>(config.GetConfigurationSection("CacheProfiles"));
var cacheProfile = cacheProfileSettings.CacheProfiles["RobotsText"];
Kiran
  • 56,921
  • 15
  • 176
  • 161