title: How to configure Kestrel urls in startup class
body: <p>I am trying to figure out the proper way to modify the URL(s) that kestrel listens on <strong><em>from the Startup class constructor</em></strong>.</p>
  
  <p><strong><em>Update: In addition to the answer below, I've come to understand that Startup class isn't used to configure Kestrel in the way I had thought. I had thought that Startup would create a single application-wide configuration object that would be located through convention, it is not. As @Tseng points out, the configuration of the Application and Hosting are separate concerns. The linked answer and accepted answer provide working examples.</em></strong></p>
  
  <p>I spun up a brand new Ubuntu 14 box in Vagrant and installed ASP.NET Core 1.1 according to the current instructions from Microsoft: <a href="https://www.microsoft.com/net/core#linuxubuntu" rel="noreferrer">https://www.microsoft.com/net/core#linuxubuntu</a></p>
  
  <p>I ran:</p>
  
  <ul>
  <li>dotnet new -t web</li>
  <li>dotnet restore</li>
  <li>dotnet run</li>
  </ul>
  
  <p>This listens on <a href="http://localhost:5000" rel="noreferrer">http://localhost:5000</a> by default. In Program.cs I can call app.UseUrls("http://*:5001) and this works to change the URL and port.</p>
  
  <p>What I really want is to change the URLs by convention through adding a new JSON setting file in the Startup class, so I followed examples and created a hosting.JSON file with this (note: I've tried "server.urls" and "urls" as the key).</p>
  
  <pre><code>{
    urls: "http://*:5001"
  }
  </code></pre>
  
  <p>In Startup.cs below the default lines for adding the appsettings.json files I added .AddJsonFile("hosting.json", optional: false); (optional: false to make sure it was picking up the file)</p>
  
  <pre><code>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)
    .AddJsonFile("hosting.json", optional: false);
    if (env.IsDevelopment())
    {
      // For more details on using the user secret store see https://go.microsoft.com/fwlink/?LinkID=532709
      builder.AddUserSecrets();
    }
  
    builder.AddEnvironmentVariables();
    Configuration = builder.Build();
  }
  </code></pre>
  
  <p>I have verified that the setting is there after the configuration builds, but it is not picked up or used when the host is built in the Program.cs</p>
  
  <pre><code>public class Program
  {
    public static void Main(string[] args)
    {
      var host = new WebHostBuilder()
      .UseKestrel()
      .UseContentRoot(Directory.GetCurrentDirectory())
      .UseIISIntegration()
      .UseStartup&lt;Startup&gt;()
      .Build();
  
      host.Run();
    }
  }
  </code></pre>
  
  <p>and the application still starts listening on localhost:5000. It is my (likely incorrect) understanding that by having a setting properly named with the correct key the WebHostBuidler should pick it up and use it.</p>
  
  <p><strong><em>I've seen other examples that basically get rid of the startup class and create the configuration in Program.cs where it can then be passed into a call to UseConfiguration but again my understanding is that using a Startup class should do this by convention.</em></strong></p>
  
  <p>Essentially <strong><em>I'd like to keep Startup and Program separate</em></strong>, add a hosting.JSON file to the configuration with URLs, and have it picked up and used without needing to call UseUrls, UseConfiguration, etc.</p>
  
  <p>Am I missing something obvious, or trying to do something that is not actually correct?</p>
  
  <p>To explain why this is not a duplicate as per my comment:</p>
  
  <ul>
  <li><p>That post specifically says "launcherSettings is for Visual Studio F5". I am using the command line on a Linux box. Nothing to do with VS.</p></li>
  <li><p>The solution provided in that post moved the configuration build into the main method. I specifically state that I want to build my configuration in the Startup class just like the default "dotnet new -t web" project.</p></li>
  </ul>
  
  <p><strong><em>I do not feel this is a duplicate, and am still reviewing the ASP.NET Core source to see if this is possible.</em></strong></p>
  
  <p>In regards to the correct key:</p>
  
  <p><a href="https://github.com/aspnet/Hosting/blob/b6da89f54cff11474f17486cdc55c2f21f2bbd6b/src/Microsoft.AspNetCore.Hosting.Abstractions/WebHostDefaults.cs" rel="noreferrer">https://github.com/aspnet/Hosting/blob/b6da89f54cff11474f17486cdc55c2f21f2bbd6b/src/Microsoft.AspNetCore.Hosting.Abstractions/WebHostDefaults.cs</a></p>
  
  <pre><code>namespace Microsoft.AspNetCore.Hosting
  {
    public static class WebHostDefaults
    {
      public static readonly string ApplicationKey = "applicationName";
      public static readonly string StartupAssemblyKey = "startupAssembly";
      public static readonly string DetailedErrorsKey = "detailedErrors";
      public static readonly string EnvironmentKey = "environment";
      public static readonly string WebRootKey = "webroot";
      public static readonly string CaptureStartupErrorsKey = "captureStartupErrors";
      public static readonly string ServerUrlsKey = "urls";
      public static readonly string ContentRootKey = "contentRoot";
    }
  }
  </code></pre>
  
  <p><a href="https://github.com/aspnet/Hosting/blob/80ae7f056c08b740820ee42a7df9eae34541e49e/src/Microsoft.AspNetCore.Hosting/Internal/WebHost.cs" rel="noreferrer">https://github.com/aspnet/Hosting/blob/80ae7f056c08b740820ee42a7df9eae34541e49e/src/Microsoft.AspNetCore.Hosting/Internal/WebHost.cs</a></p>
  
  <pre><code>public class WebHost : IWebHost
  {
    private static readonly string DeprecatedServerUrlsKey = "server.urls";
  </code></pre>