Copied to clipboard

Flag this post as spam?

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


  • Murray Roke 503 posts 966 karma points c-trib
    May 01, 2023 @ 01:38
    Murray Roke
    0

    appsettings.{Environment.MachineName}.json

    Does anyone know how to get this working in umbraco? I found some generic .net core instructions but not for Umbraco9+

    I want appsettings.my-machine-name.json just for me so can have custom path & connection strings that differ from the other devs in my team.

    I found this: https://andrewlock.net/how-to-use-machine-specific-configuration-with-asp-net-core/

    But umbraco seems to differ from that pattern of startup.cs

  • Kevin Jump 2343 posts 14890 karma points MVP 8x c-trib
    May 01, 2023 @ 10:54
    Kevin Jump
    101

    Hi,

    yes by default Umbraco is using the default settings here, so looking for command line, envrionmental variables and then the files.

    you can add the machine one if you want in program.cs

    adding

           .ConfigureAppConfiguration(config => {
                    config.AddJsonFile($"appsettings.{Environment.MachineName}.json", optional: true);
            })
    

    seems to work, this would then be after the ConfigureUmbracoDefaults() section For example:

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureUmbracoDefaults()
                .ConfigureAppConfiguration(config => {
                    config.AddJsonFile($"appsettings.{Environment.MachineName}.json", optional: true);
                })
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStaticWebAssets();
                    webBuilder.UseStartup<Startup>();
                });
    

    I am not sure what impact this might have on upgrades, so its worth making sure you check when you do upgrade, but in general you can make changes to these files between major versions with no issues (and usually major versions too).

  • Murray Roke 503 posts 966 karma points c-trib
    May 03, 2023 @ 05:59
    Murray Roke
    1

    Awesome I knew it was possible, I just couldn't find the right spot. Cheers.

  • Owain Jones 60 posts 410 karma points MVP 4x c-trib
    May 01, 2023 @ 14:21
    Owain Jones
    1

    Just thought I'd add how I achieve something similar on my projects.

    I use the "User Secrets" functionality in Visual Studio. Screenshot of Visual Studio with the right-click context menu open on the project highlighting the Manage User Secrets option Any app settings added here will override whatever is in your appsettings.{configuration}.json

    The secrets.json that's created is stored at C:\Users\[username]\AppData\Roaming\Microsoft\UserSecrets\[guid] so it is separate from your local repo.

    I know it's not exactly what you asked for, but it's a good way of using different app settings without worrying about accidentally committing them to your repository.

  • Murray Roke 503 posts 966 karma points c-trib
    May 03, 2023 @ 05:59
    Murray Roke
    1

    That looks handy too, Thanks Owain

  • Murray Roke 503 posts 966 karma points c-trib
    May 04, 2023 @ 01:56
    Murray Roke
    1

    Hi Owain I tried this and it appears that the order of over-ride is like so: Which is ok, I was just expecting the last 2 in the other order :-)

    appsettings.json
    appsettings.Development.json
    secrets.json
    appsettings.{Environment.MachineName}.json

  • Owain Jones 60 posts 410 karma points MVP 4x c-trib
    May 04, 2023 @ 07:20
    Owain Jones
    0

    Huh, interesting, I would've expected it to be the other way around too!

Please Sign in or register to post replies

Write your reply to:

Draft