Copied to clipboard

Flag this post as spam?

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


  • npack 51 posts 297 karma points
    25 days ago
    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 51 posts 297 karma points
    24 days ago
    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 2342 posts 14889 karma points MVP 8x c-trib
    24 days ago
    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 51 posts 297 karma points
    24 days ago
    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?

Please Sign in or register to post replies

Write your reply to:

Draft