Copied to clipboard

Flag this post as spam?

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


  • limin 27 posts 149 karma points
    Mar 20, 2023 @ 19:35
    limin
    0

    web.config Transform equivalency in Umbraco 10

    Hi,

    In my old project in Visual Studio, I can have Web.config, and its transform files like Web.Debug.config, Web.Staging.config, Web.Release.config when I publish to Staging and Production environment, some value will be different between the environment depending on setting in the config in the transform files

    what is its equivalency in Umbraco 10?

    I read about setting ASPNETCORE_ENVIRONMENT in launchSettings.json, and adding appsettings.Development.json, appsettings.Staging.json and appsettings.Release.json, but I can't seem to connect the dots

    In the end, what I want is, for example, when I publish my website to Staging, my website should connect to my Staging database, when I publish website to Production, my website should connect to my Production database. These 2 environments are physically on same server in same IIS.

    Can someone point some direction on documents and/or code?

    Many thanks

  • Tor Langlo 189 posts 532 karma points
    Mar 21, 2023 @ 03:25
    Tor Langlo
    0

    In an Asp.Net Core application you can set the value of the IHostEnvironment.EnvironmentName property. If the value of this property corresponds to the "middle" part of appSettings.{EnvironmentName}.json, then this appsettings file will be merged with the default appSettings.json file.

    The most common ways of setting IHostEnvironment.EnvironmentName is

    1. Setting the ASPNETCORE_ENVIRONMENT variable in launchSettings.json in Visual Studio.
    2. Setting the environmentVariable element in web.config.
    3. Setting ASPNETCORE_ENVIRONMENT variable in the host process/op.system environment.

    Here's the place to set it in web.config (I have removed some attributes for clarity):

    <configuration>
      <location .....>
        <system.webServer>
          <aspNetCore ......>
            <environmentVariables>
              <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
            </environmentVariables>
          </aspNetCore>
        </system.webServer>
      </location>
    </configuration>
    

    The Microsoft documentation has the details.

  • limin 27 posts 149 karma points
    Mar 24, 2023 @ 18:19
    limin
    0

    Thanks you very much for your info, I have been reading things similar but I must have missed some key point. Can I confirm one thing at a time ?

    On the server, not on my local in Visual Studio, Is that true that the website is only looking into setting in the appSetting.json? although I have these files in the folder (appsettings.json, appsettings.Development.json, appsettings.Staging.json, appsettings.production.json) my website is only reading setting in the appSetting.json

    is this correct? thanks

  • Huw Reddick 1702 posts 5999 karma points MVP c-trib
    Mar 27, 2023 @ 12:40
    Huw Reddick
    0

    these files in the folder (appsettings.json, appsettings.Development.json, appsettings.Staging.json, appsettings.production.json) my website is only reading setting in the appSetting.json

    is this correct? thanks

    It depends on the envirnment variables set.

    Production is the default value if DOTNETENVIRONMENT and ASPNETCOREENVIRONMENT have not been set. Apps deployed to Azure are Production by default.

    so if you uploaded all those files to your webserver and have not set variables, then it will do this

    Settings can be configured in multiple places. Here are the priorities used when determining the value of a setting, listed from high (1) to low (3):

    1. Value within the appsettings.Production.json file
    2. Value within the appsettings.json file
    3. Default value set in the code. Used only if a specific setting can't be found within appsettings.Production.json or appsettings.json.

    So by default appsettings.Development.json, appsettings.Staging.json should be ignored.

  • Huw Reddick 1702 posts 5999 karma points MVP c-trib
    Mar 27, 2023 @ 12:48
    Huw Reddick
    0

    One thing you need to be aware of, especially when developing is that settings are replaced individually, so if you add the below for example to appsettings.json

        "Smtp": {
          "From": "huw@***********",
          "Host": "mail.********.***",
          "Port": 587,
          "Username": "*************",
          "Password": "***********",
          "DeliveryMethod": "Network",
          "SecureSocketOptions": "StartTls"
        },
    

    But add this in your appsettings.Development. json

        "Smtp": {
          "From": "huw@********",
          "Host": "127.0.0.1",
          "Port": 25,
          "DeliveryMethod": "Network"
        },
    

    You will get an error because it will still try to use "StartTls"

    appsettings.json should just contain a very basic set of settings.

  • limin 27 posts 149 karma points
    Mar 27, 2023 @ 17:33
    limin
    0

    Got it, thanks

  • Yakov Lebski 539 posts 2101 karma points
    Mar 26, 2023 @ 23:54
    Yakov Lebski
    100

    if you want to add ASPNETCORE_ENVIRONMENT to web.config you can do it by adding to publish profile

    <PropertyGroup>
        <EnvironmentName>Test</EnvironmentName>
    </PropertyGroup>
    
  • limin 27 posts 149 karma points
    Mar 27, 2023 @ 17:27
    limin
    101

    so, I added the following in the publish profile

    <PropertyGroup>
        <EnvironmentName>Staging</EnvironmentName>
    </PropertyGroup>
    

    then I published to staging website it worked

    I can see that config from the appsettings.Staging.json replaced the one in the appsettings.json

    Thank you!

  • limin 27 posts 149 karma points
    Mar 27, 2023 @ 16:39
    limin
    0

    ok great, thank you all for helping, I am starting to get it, on my local, in the launchSettings.json, when I set "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } I am getting config from the appsettings.Development.json

    when I set "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Staging" } I am getting config from the appsettings.Staging.json

    when I set "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Production" } I am getting config from the appsettings.Production.json

    this is exciting, now I am going to see how the publish works. thanks again

  • Tor Langlo 189 posts 532 karma points
    Mar 27, 2023 @ 16:57
    Tor Langlo
    0

    And don't forget, you get the defaults from appSettings.json, with overrides and additions from the Development/Staging/Production json file.

  • limin 27 posts 149 karma points
    Mar 27, 2023 @ 17:20
    limin
    0

    thanks, yes, what I actually meant when I said "I am getting config from the appsettings.Staging.json" is that "the config in appsettings.Staging.json is replacing the one in the appsettings.json"

  • limin 27 posts 149 karma points
    Mar 27, 2023 @ 17:33
    limin
    0

    Thank you all again, my problem is solved!

Please Sign in or register to post replies

Write your reply to:

Draft