A Guide to launchSettings.json in ASP.NET

If you’re building a web application using ASP.NET Core, you may have come across the file launchSettings.json. This file is used to configure how your application is launched during development. Since it comes as part of the default templates, most of us simply overlook it. While we may never have to touch this file in most cases, it is good to understand its purpose so that we can make adjustments to it, if such a need ever arises. In this post, we’ll take a closer look at it and demystify its inner workings.

What is launchSettings.json?

launchSettings.json is a configuration file used by ASP.NET Core to specify how an application is launched. It contains information such as the command to start the application, the environment variables to set, and the ports to use. This file is used by Visual Studio and the .NET Core CLI when launching the application during development.

Understanding the structure of launchSettings.json

Let’s take a look at the structure of launchSettings.json. Here’s an example:

{
  "profiles": {
    "MyApplication": {
      "commandName": "Project",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "http://localhost:5000/"
    }
  }
}

The profiles property contains a collection of named profiles. In this example, there is only one profile called MyApplication. Each profile has a set of properties that specify how the application should be launched.

The commandName property specifies how the application should be started. In this case, the value is Project, which means that the application should be started using the project’s output assembly.

The launchBrowser property specifies whether the default browser should be launched when the application is started. If this property is set to true, the browser will be launched automatically.

The environmentVariables property contains a collection of environment variables that should be set when the application is started. In this example, the ASPNETCORE_ENVIRONMENT variable is set to Development.

The applicationUrl property specifies the URL that the application should be accessible from. In this case, the URL is http://localhost:5000/.

Using launchSettings.json for App Initialization

launchSettings.json is used by Visual Studio and the .NET Core CLI when launching the application during development. When you run your application in Visual Studio, it will use the launchSettings.json file to determine how to launch the application.

You can also use launchSettings.json when running your application from the command line using the dotnet run command. By default, dotnet run will use the Development environment, but you can specify a different environment using the ASPNETCORE_ENVIRONMENT environment variable.

dotnet run --environment Production

Closing Remarks

This file should be committed to source control, along with the rest of our code. It allows team members to utilize the same launch settings across the team. While we may never have to touch it, we have learned how the framework and tools like VSCode and Visual Studio use it to launch your application. You may have wondered how your browser opened up with your dotnet site when you issued a dotnet run at the terminal. I hope this post shed some light on how some of those things are wired up.

Leave a Comment

Your email address will not be published. Required fields are marked *