'Running .NET 5 Api under Kestrel from Visual Studio 2019 ignores launchSettings.json applicationUrl setting

We are having an "only on my machine" issue that nobody can figure out.

We have a .NET 5 Api that we run under Kestrel for development purposes. On every other dev VM running the API in Visual Studio picks up on the values in launchSettings.json and launches on the correct ports. However on a the VM in question, it only launches on the default 5000/5001 ports.

This is the entry for the launchSettings.json file.

"profiles": {
    "My.Api": {
        "commandName": "Project",
        "environmentVariables": {
            "LogStorageAccountConnectionString": "UseDevelopmentStorage=true",
            "ASPNETCORE_ENVIRONMENT": "Development"
        },
        "applicationUrl": "https://localhost:7001;http://localhost:7000"
    }
}

I know there is a way to tell dotnet run to explicitly ignore launchSettings.json but could not find a setting in Visual Studio that would trigger that behavior.

We also thought it might be a permissions issue on the folder because they were a bit weird so we cloned our repo into a new folder and set up the Api again to be run locally. It still ran on 5000/5001.

The lot of us are stumped. Any ideas?



Solution 1:[1]

I had exactly the same problem also running .NET 5 API (and also a Blazor project) under Kestrel with Visual Studio. For me however it came suddenly - I was running the project successfully for a couple of weeks and then one day it started refusing to honour launchSettings.json and keep running on default 5000/5001.

I have solved this by upgrading Visual Studio from 16.8.3 to 16.8.4. However since I did not have problems initially with 16.8.3 I suspect the problem is not about Visual Studio version. I think that my original installation got corrupted somehow and the upgrade just fixed the corruption.

BTW my launchSetting.json were honoured when I ran the project from command line using dotnet run. I could also use a workaround by setting the port in Program.cs

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
                webBuilder.UseUrls("https://localhost:6000");
            });

Solution 2:[2]

We ran into this issue too with both .NET 5 and .NET 6 in Visual Studio 2022, and the only thing that worked was to set the (apparently undocumented) variable Urls in the environmentVariables section of launchSettings.json. In other words:

"profiles": {
    "My.Api": {
        "commandName": "Project",
        "environmentVariables": {
            "LogStorageAccountConnectionString": "UseDevelopmentStorage=true",
            "ASPNETCORE_ENVIRONMENT": "Development",
            "Urls": "https://localhost:7001;http://localhost:7000"
        }
    }
}

Sadly VS's "Launch Profiles" dialog does not pick up this value nor allow you to set it, but editing launchSettings.json manually isn't too much of a pain.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dhrumil shah
Solution 2 Ian Kemp