2016-09-19 10 views
3

次のコードを持つAsp.netコアアプリケーションがあります。`UseUrl(...)`の設定ファイルの設定を使用しますか?

Program.csの

public class Program 
{ 
    public static void Main(string[] args) 
    { 
     var host = new WebHostBuilder() 
      .UseKestrel() 
      .UseContentRoot(Directory.GetCurrentDirectory()) 
      .UseUrls("http://*:5000") 
      ...... 

私はハードコードするように設定ファイルからそれを読むためにどのようにポート番号5000をしたくないですか?

startup.csは、いくつかの設定にconfigファイルを使用します。 codeはprogram.csに複製する必要がありますか?しかし、どのようにIHostingEnvironment envを取得するには?

Startup.cs

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

    builder.AddEnvironmentVariables(); 
    Configuration = builder.Build(); 
} 
+0

ASP.NETでケストレルのURLを設定する方法[記事を参照してください。コア](http://benfoster.io/blog/how-to-configure-kestrel-urls-in-aspnet-core-rc2) –

答えて

3

メインメソッドでIConfigurationのインスタンスを作成し、ホスト構成のためにそれを使用することが可能です。あなたが直接.UseConfiguration(config)拡張メソッドを使用することができます。また、あなたは直接お

public static void Main(string[] args) 
{ 
    var config = new ConfigurationBuilder() 
        .SetBasePath(Directory.GetCurrentDirectory()) 
        //.AddJsonFile("hosting.json", optional: true) 
        //.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) 
        //.AddCommandLine(args) 
        //.AddEnvironmentVariables() 
        .Build(); 

    var host = new WebHostBuilder() 
      .UseUrls(<values from config>); 
} 

また、することができます。この場合、

var host = new WebHostBuilder() 
       .UseConfiguration(config) 
       .UseKestrel() 
       .UseContentRoot(Directory.GetCurrentDirectory()) 
       .UseIISIntegration() 
       .UseStartup<Startup>() 
       .Build(); 

を、あなたの設定ファイルは、「server.urls」パラメータを持っている必要があります。あなたのケースのために:

​​

また、あなたが直接アプリを実行すると、コマンドラインからポートを渡すことができることに、注意してください。

dotnet run --server.urls=http://0.0.0.0:5001 
+0

ポート番号を変更するのがなぜ難しいのですか?既存の設定ファイルがありますか?私はlaunchSettings.jsonについて読んでいますか? – ca9163d9

+0

@ dc7a9163d9実際には "server.urls"設定パラメータでこれをすばやく変更できます。私は答えを更新しました。 – Set

+0

ありがとうございます。どのようにSSLを有効にする? 'dotnet myapp.dll'を使って実行しています。 – ca9163d9

関連する問題