Vikram
Vikram

Reputation: 604

Kestrel configuration to use a specific port + url

I am using Asp.Net core 2.0.2 on Win 7 with VS2017 (15.3.5).

My current Kestrel configuration looks like this:

return WebHost.CreateDefaultBuilder(args)
    .UseStartup<Startup>()
    .ConfigureAppConfiguration((hostContext, config) =>
    {
        var envName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

        config.Sources.Clear();
        config.AddJsonFile("appsettings.json", optional : false);
        config.AddJsonFile($"appsettings.{envName}.json", optional : false);
        config.AddEnvironmentVariables();
    })
    .UseKestrel(options =>
    {
        options.Listen(IPAddress.Loopback, 5859);
    })
    .UseContentRoot(pathToContentRoot)
    .Build();

This obviously listens on http://localhost:5859. I want to configure Kestrel so that it ONLY listens on a custom URL such as http://localhost:5859/MyNewApp. How do I do that?

(With Core 1.0, I had used UseUrls("http://localhost:5859/MyNewApp") which partially did the job. It would listen on http://localhost:5859 as well as http://localhost:5859/MyNewApp. Doing the same in Core 2.0.2 results in an exception:

System.InvalidOperationException: A path base can only be configured using IApplicationBuilder.UsePathBase())

Upvotes: 2

Views: 4266

Answers (1)

Marc LaFleur
Marc LaFleur

Reputation: 33094

With 2.0 you need to leverage UsePathBase as UseUrls was removed from Kestrel. You'll want to do this in your Configure method at startup:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UsePathBase("/MyNewApp"); 
}

Upvotes: 3

Related Questions