How to convert appsettings.json to a .NET Core MVC project?

I added additional json configuration files to my project

appsettings.DEV.json
appsettings.QA.json

and loaded them into an Startupenvironment dependent function :

public Startup(IHostingEnvironment env)
{
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
    ...

And I understand how to change the environment: change the value of the environment variable ASPNETCORE_ENVIRONMENTin the project properties. However, apparently, it is not possible to specify various environment variables depending on the configuration, the drop-down list is marked as "N / A" and is disabled.

The only option I see is to manually change the value of the environment variable to change which application settings are used. I am sure there is a way to do this automatically, otherwise how would you use CI? (besides using a script to change the environment variable, there should be an easier way).

- : DEV, QA PROD. DEV QA , , , .

Project Properties: Debug section

+6
6

Tsengs, . , ( ), , .

IIS ASPNETCORE_ENVIRONMENT

, :

  • IIS Configuration Editor.
  • Configuration Editor
  • system.webServer/aspNetCore (RC2 RTM) system.webServer/httpPlatform (RC1) Section combobox
  • Applicationhost.config ... From combobox.
  • enviromentVariables .
  • .
  • "".

applicationHost.config ( C:\Windows\System32\inetsrv\config\applicationHost.config

<Configuration>, "my-iis-site" - IIS.

<location path="my-iis-site">
    <system.webServer>
        <aspNetCore>
            <environmentVariables>
                <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="DEV" />
            </environmentVariables>
        </aspNetCore>
    </system.webServer>
</location>
+7

, , .UseEnvironment(environmentName) WebHostBuilder ( ).

, - , :

public static void Main(string[] args)
{
    string environmentName;
#if DEBUG
    environmentName = "Development";
#elif STAGING
    environmentName = "Staging";
#elif RELEASE
    environmentName = "Production";
#endif

    var host = new WebHostBuilder()
        .UseKestrel()
        .UseEnvironment(environmentName)
        .UseContentRoot(Directory.GetCurrentDirectory())
        .UseIISIntegration()
        .UseStartup<Startup>()
        .UseApplicationInsights()
        .Build();

    host.Run();
}
+11

program.cs, , .

program.cs :

public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .Build();

:

public static IWebHostBuilder CreateDefaultBuilder(string[] args)
        {
            var builder = new WebHostBuilder();

            ...

            builder.ConfigureAppConfiguration((hostingContext, config) =>
                {
                    var env = hostingContext.HostingEnvironment;

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

        ...

            return builder;
        }

, , , appsettings.YourEnvionronmentalVariable.json .

: , :

var appSettings = Configuration.GetSection("AppSettings").Get<AppSettings>();

:

var appSettings = new AppSettings();
Configuration.Bind("AppSettings", appSettings);
return appSettings;

.

+3

, , "launchSettings.json". .

Visual Studio URL- F5 .

, . , .

Windows (Commandline, cmd.exe)

setx ASPNETCORE_ENVIRONMENT "Development"

Windows (Powershell)

$Env:ASPNETCORE_ENVIRONMENT = "Development"

Linux

export ASPNETCORE_ENVIRONMENT="Development"

Linux ( )

ASPNETCORE_ENVIRONMENT="Development" dotnet run

, ( Linux, ). , IIS , answer , IIS

0

azure, ASPNETCORE_ENVIRONMENT webapp, . {value}.json file

0

, . .

1. (.CsProj)

MSBuild EnvironmentName , . web.config .

(*.csProj) XML.

    <!-- Custom Property Group added to add the Environment name during publish
  The EnvironmentName property is used during the publish for the Environment variable in web.config
  -->
  <PropertyGroup Condition=" '$(Configuration)' == '' Or '$(Configuration)' == 'Debug'">
    <EnvironmentName>Development</EnvironmentName>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)' != '' AND '$(Configuration)' != 'Debug' ">
    <EnvironmentName>'$(Configuration)'</EnvironmentName>
  </PropertyGroup>

Development Debug . . ASPNETCORE_ENVIRONMENT . , CsProj.

2. EnvironmentName .

<EnvironmentName> . , Properties/PublishProfiles/{profilename.pubxml} web.config .

<PropertyGroup>
  <EnvironmentName>Development</EnvironmentName>
</PropertyGroup>

enter image description here

, , EnvironmentName *.pubxml.

3. dotnet publish

, EnvironmentName dotnet publish. Development web.config.

dotnet publish -c Debug -r win-x64/p:EnvironmentName=Development

0

Source: https://habr.com/ru/post/1673424/


All Articles