Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • npack 68 posts 378 karma points
    Oct 28, 2024 @ 20:07
    npack
    0

    Changing Configuration From Code Instead of AppSettings.json?

    I want to change the Umbraco/CMS/Global/ReservedPaths configuration setting from inside my program.cs instead of doing it in appsettings.json.

    Why? I have so many reserved paths that the json is getting unwieldy

    "ReservedPaths": "~/path1,~/path2,~/path3",  /* but with longer paths and many more
    

    Does anybody know if this is possible? It seems like during startup in program.cs it would be a good opportunity.

  • npack 68 posts 378 karma points
    Oct 29, 2024 @ 15:12
    npack
    0

    Which service am I to retrieve from GetRequiredService?

    There is a GlobalSettings configuration class for that config section but I must be doing things incorrectly since my runtime change doesn't seem to override the appsettings.json values.

    This is what DOESNT work:

            var globalSection = umbracoBuilder.Config.GetSection("Umbraco:CMS:Global").Get<GlobalSettings>();
            if (globalSection != null)
            {
                globalSection.ReservedPaths = globalSection.ReservedPaths.Replace("somevalue", "someothervalue");
                umbracoBuilder.Config.GetSection("Umbraco:CMS:Global:ReservedPaths").Value = globalSection.ReservedPaths;
            }
    
  • Kevin Jump 2348 posts 14896 karma points MVP 8x c-trib
    Oct 29, 2024 @ 19:44
    Kevin Jump
    0

    You can use PostConfigure method in a composer to change the settings values (this is called when the site loads and if the file is changed, and the settings reloaded).

    e.g.

    public class MyReservePathComposer : IComposer
    {
        public void Compose(IUmbracoBuilder builder)
        {
            builder.Services.PostConfigure<GlobalSettings>(settings =>
            {
                settings.ReservedPaths = "one, two, three";
            });
        }
    }
    

    as a composer it will get wrapped into umbraco and called when the site starts.

  • npack 68 posts 378 karma points
    Oct 29, 2024 @ 20:31
    npack
    0

    Thanks Kevin,

    I dropped in this composer class but its 'Compose' method didn't seem to get hit. I'm using Umbraco 14. I haven't used a composer before, but I thought that it would be found and registered automatically on startup. Is that correct?

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies