0

What I'm trying to do, ultimately, is access the CacheSettingsPart so that I can programmatically add some ignored URLs to the output caching config.

The relevant admin controller already achieves this with:

   var settings = Services.WorkContext.CurrentSite.As<CacheSettingsPart>();
   settings.IgnoredUrls = model.IgnoredUrls;

I need something similar for my own method, but when I try and inject IOrchardServices, the WorkContext is null, meaning I don't have access to the CurrentSite.

I need suggestions of achieving this with an alternative approach or, ideally, a way of accessing the CurrentSite/CacheSettingsPart for me to amend the IgnoredUrls.

EDIT

var query = Services.ContentManager.Query<CacheSettingsPart>();
var cacheSettingsPart = query.List().First();

The above seems to be giving me what I need, I'll now test whether amending IgnoredUrls persists or not.

Dan
  • 89
  • 7

2 Answers2

3

The WorkContext, as far as I can see, hasn't been created at the point of the Migration being run.

You could get the first SettingsPart as you suggest - it's probably not used for anything except the current site, though if you had multiple tenants, then I think you might run into trouble.

An alternative would be to inject ISiteService into your migrations class.

You can then do

var site = _siteService.GetSiteSettings();
var cacheSettings = site.As<CacheSettingsPart>();
David Cornish
  • 371
  • 1
  • 7
0
  var query = Services.ContentManager.Query<CacheSettingsPart>();
  var cacheSettingsParts = query.Slice(1);

  if(cacheSettingsParts.Any())
  {
     _signals.Trigger(CacheSettingsPart.CacheKey);
     cacheSettingsParts.First().IgnoredUrls = "/dans-test";
  }

The above works, but I appreciate it might not be the best approach so I'm open to other suggestions.

Dan
  • 89
  • 7