asp.net-core                
            複数の環境の設定
        
        
            
    サーチ…
環境ごとの設定
それぞれの環境に対して、個別のappsettingsを作成する必要があります。{EnvironmentName} .jsonファイル:
- appsettings.Development.json
- appsettings.Staging.json
- appsettings.Production.json
次に、project.jsonファイルを開いて、 "publishOptions"セクションの "include"に含めます。これは、公開時に含まれるすべてのファイルとフォルダを一覧表示します。
"publishOptions": {
  "include": [
    "appsettings.Development.json",
    "appsettings.Staging.json",
    "appsettings.Production.json"
    ... 
  ]
}
最後のステップ。 Startupクラスに次の行を追加します。
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
設定ソースを設定するコンストラクタで
var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
            .AddEnvironmentVariables();
コードから環境名の取得/確認
必要なのは、 IHostingEnvironment型の変数です。 
- 環境名を取得する: - env.EnvironmentName
- 定義済みの - Development、- Staging、- Production環境では、 HostingEnvironmentExtensionsクラスの拡張メソッドを使用するのが最善の方法です- env.IsDevelopment() env.IsStaging() env.IsProduction()
- 大文字と小文字を正しく無視する( HostingEnvironmentExtensionsの別の拡張メソッド: - env.IsEnvironment("environmentname")
- case sensitive variant - env.EnvironmentName == "Development"
複数の環境の設定
この例では、Dependency Injectionの設定が異なる複数の環境を構成し、1つのStartupクラスでミドルウェアを分離する方法を示しStartup 。 
 public void Configure(IApplicationBuilder app)およびpublic void ConfigureServices(IServiceCollection services)メソッドのほかに、 Configure{EnvironmentName}およびConfigure{EnvironmentName}Servicesを使用して環境依存の設定を行うことができます。 
このパターンを使用するとif/elseロジックに1つのメソッド/ Startupクラスを渡すことを避け、クリーンで分離したままにしておきStartup 。 
public class Startup
{
    public void ConfigureServices(IServiceCollection services) { }
    public void ConfigureStaggingServices(IServiceCollection services) { }
    public void ConfigureProductionServices(IServiceCollection services) { }
    
    public void Configure(IApplicationBuilder app) { }
    public void ConfigureStagging(IApplicationBuilder app) { }
    public void ConfigureProduction(IApplicationBuilder app) { }
}
 Configure{Environmentname}またはConfigure{Environmentname}Servicesが見つからない場合、それぞれConfigureまたはConfigureServicesフォールバックします。 
同じセマンティクスはStartupクラスにも適用されStartup 。 StartupProductionは、 ASPNETCORE_ENVIRONMENT変数がProduction設定されているときに使用され、 StaggingまたはDevelopmentのときにStartupに戻りStartup 
完全な例:
public class Startup
{
    public Startup(IHostingEnvironment hostEnv)
    {
        // Set up configuration sources.
        var builder = new ConfigurationBuilder()
            .SetBasePath(hostEnv.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddJsonFile($"appsettings.{hostEnv.EnvironmentName}.json", optional: true, reloadOnChange: true);
        if (hostEnv.IsDevelopment())
        {
            // This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately.
            builder.AddApplicationInsightsSettings(developerMode: true);
        }
        builder.AddEnvironmentVariables();
        Configuration = builder.Build();
    }
    public IConfigurationRoot Configuration { get; set; }
    // This method gets called by the runtime. Use this method to add services to the container
    public static void RegisterCommonServices(IServiceCollection services) 
    {
        services.AddScoped<ICommonService, CommonService>();
        services.AddScoped<ICommonRepository, CommonRepository>();
    }
    
    public void ConfigureServices(IServiceCollection services)
    {
        RegisterCommonServices(services);
        
        services.AddOptions();
        services.AddMvc();
    }
    public void ConfigureDevelopment(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();
        app.UseBrowserLink();
        app.UseDeveloperExceptionPage();
        app.UseApplicationInsightsRequestTelemetry();
        app.UseApplicationInsightsExceptionTelemetry();
        app.UseStaticFiles();
        app.UseMvc();
    }
    // No console Logger and debugging tools in this configuration
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddDebug();
        app.UseApplicationInsightsRequestTelemetry();
        app.UseApplicationInsightsExceptionTelemetry();
        app.UseStaticFiles();
        app.UseMvc();
    }
}
ビュー内の環境固有のコンテンツをレンダリングする
いくつかのコンテンツのみを表示する必要があるかもしれません。この目標を達成するには、環境タグヘルパーを使用できます。
<environment names="Development">
    <h1>This is heading for development environment</h1>
</environment>
<environment names="Staging,Production">
    <h1>This is heading for Staging or production environment</h1>
</environment>
 Environmentタグヘルパーは、現在の環境がnames属性を使用して指定された環境の1つに一致する場合にのみ内容をレンダリングします。 
コマンドラインから環境変数を設定する
 Development環境に環境を設定するには
 SET ASPNETCORE_ENVIRONMENT=Development 
Asp.Net Coreアプリケーションを実行すると、定義された環境になります。
注意
- 等号記号=前後には空白を入れないでください。
- 設定が維持されないため、アプリケーションを実行する前にコマンドプロンプトを閉じるべきではありません。
PowerShellから環境変数を設定する
PowerShellを使用する場合は、setx.exeを使用して環境変数を永続的に設定できます。
- PowerShellを起動する 
- 次のいずれかを入力します。 - setx ASPNETCORE_ENVIRONMENT "開発" - setx ASPNETCORE_ENVIRONMENT "ステージング" 
- PowerShellを再起動する 
web.configのASPNETCORE_ENVIRONMENTの使用
環境変数からASPNETCORE_ENVIRONMENTを使い、アプリケーションのweb.configからASPNETCORE_ENVIRONMENTを使いたくない場合は、web.configを次のように変更します。
<aspNetCore processPath=".\WebApplication.exe" arguments="" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false">
  <environmentVariables>
    <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
  </environmentVariables>
</aspNetCore>