How to update appsettings.json based on publish profile using .NET Core?












0















I'm currently making the switch from .NET Framework to .NET Core. In the past, all of my application settings were in the Web.config file. When I added a new publish profile, I could right click and select 'Add Config Transform' which would generate a nested Web.{Profile}.config file under Web.config where I could set application settings that were specific to the respective profile.



Now, in .NET Core, I want to achieve the same effect using an appsettings.json file instead of Web.config file. How can I create an appsettings.{Profile}.json file which will be nested under my appsettings.json file and contain settings that are specific to my publish profile? Of course, I can create the file manually, but what is it that 'links' the settings so that they will override appsettings.json when the application is published? Is there a simple way to do this in Visual Studio like I described for my old .NET Framework projects? Or am I missing something?



Thanks!










share|improve this question



























    0















    I'm currently making the switch from .NET Framework to .NET Core. In the past, all of my application settings were in the Web.config file. When I added a new publish profile, I could right click and select 'Add Config Transform' which would generate a nested Web.{Profile}.config file under Web.config where I could set application settings that were specific to the respective profile.



    Now, in .NET Core, I want to achieve the same effect using an appsettings.json file instead of Web.config file. How can I create an appsettings.{Profile}.json file which will be nested under my appsettings.json file and contain settings that are specific to my publish profile? Of course, I can create the file manually, but what is it that 'links' the settings so that they will override appsettings.json when the application is published? Is there a simple way to do this in Visual Studio like I described for my old .NET Framework projects? Or am I missing something?



    Thanks!










    share|improve this question

























      0












      0








      0








      I'm currently making the switch from .NET Framework to .NET Core. In the past, all of my application settings were in the Web.config file. When I added a new publish profile, I could right click and select 'Add Config Transform' which would generate a nested Web.{Profile}.config file under Web.config where I could set application settings that were specific to the respective profile.



      Now, in .NET Core, I want to achieve the same effect using an appsettings.json file instead of Web.config file. How can I create an appsettings.{Profile}.json file which will be nested under my appsettings.json file and contain settings that are specific to my publish profile? Of course, I can create the file manually, but what is it that 'links' the settings so that they will override appsettings.json when the application is published? Is there a simple way to do this in Visual Studio like I described for my old .NET Framework projects? Or am I missing something?



      Thanks!










      share|improve this question














      I'm currently making the switch from .NET Framework to .NET Core. In the past, all of my application settings were in the Web.config file. When I added a new publish profile, I could right click and select 'Add Config Transform' which would generate a nested Web.{Profile}.config file under Web.config where I could set application settings that were specific to the respective profile.



      Now, in .NET Core, I want to achieve the same effect using an appsettings.json file instead of Web.config file. How can I create an appsettings.{Profile}.json file which will be nested under my appsettings.json file and contain settings that are specific to my publish profile? Of course, I can create the file manually, but what is it that 'links' the settings so that they will override appsettings.json when the application is published? Is there a simple way to do this in Visual Studio like I described for my old .NET Framework projects? Or am I missing something?



      Thanks!







      visual-studio asp.net-core .net-core asp.net-core-mvc appsettings






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 16 '18 at 1:58









      MatthewMatthew

      521619




      521619
























          1 Answer
          1






          active

          oldest

          votes


















          3















          but what is it that 'links' the settings so that they will override
          appsettings.json when the application is published?




          The appsettings are configured by WebHost in Program.cs



          public static IWebHostBuilder CreateWebHostBuilder(string args) =>
          WebHost.CreateDefaultBuilder(args)
          .UseStartup<Startup>();


          In the implementation of webhost.cs the framework adds the appsettings to the webhost:



          config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
          .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);


          The second line is where it adds the environment specific appsettings. Within Visual Studio this environment variable is defined in the Project Settings -> Debug page and is called ASPNETCORE_ENVIRONMENT. By default it is set to Development so when you build and run within Visual Studio the appsettings.development.json file (if it exists) will be loaded and override any matching settings in the appsettings.json file.



          When you publish your application you can override this value using an OS environment variable of the same name. There is an hierarchy of where .NET Core reads the values from and has a "last one wins " policy.



          The hierarchy is currently:





          1. Files (appsettings.json, appsettings.{Environment}.json, where {Environment} is the app's current hosting environment)

          2. Azure Key Vault

          3. User secrets (Secret Manager) (in the Development environment only)

          4. Environment variables

          5. Command-line arguments




          So when you publish your app you can override the environment on the host OS using an environment variable. When .NET Core starts your published app it will read that variables and load the appropriate appsettings.{environment}.json file. IF the value is not set, or no file exists for that environment, then the settings in appsettings.json will apply.



          You can read more about the ASPNETCORE_ENVIROMENT setting here






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53330404%2fhow-to-update-appsettings-json-based-on-publish-profile-using-net-core%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            3















            but what is it that 'links' the settings so that they will override
            appsettings.json when the application is published?




            The appsettings are configured by WebHost in Program.cs



            public static IWebHostBuilder CreateWebHostBuilder(string args) =>
            WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>();


            In the implementation of webhost.cs the framework adds the appsettings to the webhost:



            config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);


            The second line is where it adds the environment specific appsettings. Within Visual Studio this environment variable is defined in the Project Settings -> Debug page and is called ASPNETCORE_ENVIRONMENT. By default it is set to Development so when you build and run within Visual Studio the appsettings.development.json file (if it exists) will be loaded and override any matching settings in the appsettings.json file.



            When you publish your application you can override this value using an OS environment variable of the same name. There is an hierarchy of where .NET Core reads the values from and has a "last one wins " policy.



            The hierarchy is currently:





            1. Files (appsettings.json, appsettings.{Environment}.json, where {Environment} is the app's current hosting environment)

            2. Azure Key Vault

            3. User secrets (Secret Manager) (in the Development environment only)

            4. Environment variables

            5. Command-line arguments




            So when you publish your app you can override the environment on the host OS using an environment variable. When .NET Core starts your published app it will read that variables and load the appropriate appsettings.{environment}.json file. IF the value is not set, or no file exists for that environment, then the settings in appsettings.json will apply.



            You can read more about the ASPNETCORE_ENVIROMENT setting here






            share|improve this answer




























              3















              but what is it that 'links' the settings so that they will override
              appsettings.json when the application is published?




              The appsettings are configured by WebHost in Program.cs



              public static IWebHostBuilder CreateWebHostBuilder(string args) =>
              WebHost.CreateDefaultBuilder(args)
              .UseStartup<Startup>();


              In the implementation of webhost.cs the framework adds the appsettings to the webhost:



              config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
              .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);


              The second line is where it adds the environment specific appsettings. Within Visual Studio this environment variable is defined in the Project Settings -> Debug page and is called ASPNETCORE_ENVIRONMENT. By default it is set to Development so when you build and run within Visual Studio the appsettings.development.json file (if it exists) will be loaded and override any matching settings in the appsettings.json file.



              When you publish your application you can override this value using an OS environment variable of the same name. There is an hierarchy of where .NET Core reads the values from and has a "last one wins " policy.



              The hierarchy is currently:





              1. Files (appsettings.json, appsettings.{Environment}.json, where {Environment} is the app's current hosting environment)

              2. Azure Key Vault

              3. User secrets (Secret Manager) (in the Development environment only)

              4. Environment variables

              5. Command-line arguments




              So when you publish your app you can override the environment on the host OS using an environment variable. When .NET Core starts your published app it will read that variables and load the appropriate appsettings.{environment}.json file. IF the value is not set, or no file exists for that environment, then the settings in appsettings.json will apply.



              You can read more about the ASPNETCORE_ENVIROMENT setting here






              share|improve this answer


























                3












                3








                3








                but what is it that 'links' the settings so that they will override
                appsettings.json when the application is published?




                The appsettings are configured by WebHost in Program.cs



                public static IWebHostBuilder CreateWebHostBuilder(string args) =>
                WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>();


                In the implementation of webhost.cs the framework adds the appsettings to the webhost:



                config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);


                The second line is where it adds the environment specific appsettings. Within Visual Studio this environment variable is defined in the Project Settings -> Debug page and is called ASPNETCORE_ENVIRONMENT. By default it is set to Development so when you build and run within Visual Studio the appsettings.development.json file (if it exists) will be loaded and override any matching settings in the appsettings.json file.



                When you publish your application you can override this value using an OS environment variable of the same name. There is an hierarchy of where .NET Core reads the values from and has a "last one wins " policy.



                The hierarchy is currently:





                1. Files (appsettings.json, appsettings.{Environment}.json, where {Environment} is the app's current hosting environment)

                2. Azure Key Vault

                3. User secrets (Secret Manager) (in the Development environment only)

                4. Environment variables

                5. Command-line arguments




                So when you publish your app you can override the environment on the host OS using an environment variable. When .NET Core starts your published app it will read that variables and load the appropriate appsettings.{environment}.json file. IF the value is not set, or no file exists for that environment, then the settings in appsettings.json will apply.



                You can read more about the ASPNETCORE_ENVIROMENT setting here






                share|improve this answer














                but what is it that 'links' the settings so that they will override
                appsettings.json when the application is published?




                The appsettings are configured by WebHost in Program.cs



                public static IWebHostBuilder CreateWebHostBuilder(string args) =>
                WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>();


                In the implementation of webhost.cs the framework adds the appsettings to the webhost:



                config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);


                The second line is where it adds the environment specific appsettings. Within Visual Studio this environment variable is defined in the Project Settings -> Debug page and is called ASPNETCORE_ENVIRONMENT. By default it is set to Development so when you build and run within Visual Studio the appsettings.development.json file (if it exists) will be loaded and override any matching settings in the appsettings.json file.



                When you publish your application you can override this value using an OS environment variable of the same name. There is an hierarchy of where .NET Core reads the values from and has a "last one wins " policy.



                The hierarchy is currently:





                1. Files (appsettings.json, appsettings.{Environment}.json, where {Environment} is the app's current hosting environment)

                2. Azure Key Vault

                3. User secrets (Secret Manager) (in the Development environment only)

                4. Environment variables

                5. Command-line arguments




                So when you publish your app you can override the environment on the host OS using an environment variable. When .NET Core starts your published app it will read that variables and load the appropriate appsettings.{environment}.json file. IF the value is not set, or no file exists for that environment, then the settings in appsettings.json will apply.



                You can read more about the ASPNETCORE_ENVIROMENT setting here







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 16 '18 at 3:22









                Simply GedSimply Ged

                2,72631625




                2,72631625
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53330404%2fhow-to-update-appsettings-json-based-on-publish-profile-using-net-core%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Bressuire

                    Vorschmack

                    Quarantine