Posted on 9/10/2021 1:04:52 PM by Admin

launchSettings.json

empty ASP.NET Core Project Directory Structure (solution explorer view)
Directory structure of an empty ASP.NET Core project (solution explorer view)
Directory structure of an empty ASP.NET Core project
Directory structure of an empty ASP.NET Core project

We were exploring all the files and folders that are present in an empty ASP.NET Core application. After analyzing Project file ({projectName}.csproj) and Dependencies, now it's time to explore the Properties folder. The properties folder contains a JSON file named as launchSettings.json.

launchSettings.json as the name suggests describes how a project can be launched. This means the file contains all the necessary settings required to launch the app. These settings are used by both Visual Studio as well as .NET Core CLI when we run the application.

This file is useful only during the development phase as it contains only the settings that are required to launch the application. It has no significance from the deployment point of view and is ignored when we publish the App.

So, we don't store any configuration settings on which our application's code depend to perform some action, for example , it is not a good place to store connection string or any such settings that will be used by the App, for such requirements we have other configuration sources such as appsettings.json or appsettings.{environment}.json.

Let's open the launchsettings.json file and analyze what settings does it store.

launchSettings.json file
launchSettings.json file

No doubt launchSettings.json file justify its purpose and stores only those settings which are required to launch the App. It contains two sections, the first one is iisSettings and the other is Profiles section.

iisSettings

iisSettings contains those settings which are required to debug the application under the IIS Express. As I already said that launchSettings.json file is not included in the published folder, so instead of saying that these settings are applicable to both IIS and IIS Express, I just said IIS express cause if you have to enable the windows authentication after configuring application on IIS then you will enable it directly from the IIS and not in launchSettings.json , however if you have to test that how it will behave in production environment without actually configuring the app on IIS then you can enable the windows authentication by setting the windows authentication option to true in the launchSettings.json.

profiles

The profile section contains the launch/debug profiles. Currently it contains two profiles IIS Express and DirectoryStructureDemo (same as the name of the project). Now, let's discuss some of its properties.

Command Name: The value of the commandName property along with the specified hosting model in the project file (projectName.csproj) determines the internal and the external (proxy) web server. We define the hosting model for the project inside the project file using the AspNetCoreHostingModel element.


<PropertyGroup>
    <TargetFramework>net5.0
    <AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel>// notice here
  </PropertyGroup>

The table shown below explains how does the runtime determines the web server for hosting the application.

CommandName impact on web server selection
CommandName vs Web Server

When we run the project through Visual studio using ctrl+F5 or directly using F5, then by default the profile with the command Name "IIS Express" is used, on the other hand if we run the project using .NET Core CLI with dotnet run command then the profile with command Name "Project" is used.

Environment Variables: The value of the environment variable "ASPNETCORE_ENVIRONMENT" determines the environment (phase) in which the project is currently running, for example if the project is running under development environment then we set it to "Development", similarly for staging environment we set it to "Staging" and for production we set it to "Production". We can also create any custom environment variable.

Now you might be thinking why do we define a value for the "ASPNETCORE_ENVIRONMENT" environment variable? The answer is, sometimes we need to conditionally execute some of the application code based on the environment, for example, during the development phase we display the developer exception page if any error occurs. I will discuss the developer exception page in detail in a later article, for now, think of it as the usual exception page that we see in the browser if any exception occurs in the application. A developer exception page contains the full stack trace that helps the developer to debug the error. But, in a production environment, we don't show the full error rather we design a beautiful error page to show it to the end-user if any errors occur, I will of course explain how do we achieve that in a later article, for now, I could only say that it is set in the Startup.cs class, something like shown below.


       public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

        }



Now, you might be thinking that when the launchSettings.json file is ignored when publish the app then how come the runtime knows about the environment variable. well, launchSettings.json file is only for the development purposes. This means, If we want to test the custom error page without actually hosting the app to the IIS or any production environment then we can set the ASPNETCORE_ENVIRONMENT to production and the .NET Runtime will think as if the application is running under production environment.

Launch Browser: launchBrowser is a Boolean property, which determines whether to launch a browser or not when we run the app. Obviously we keep its value to true all the time.

As I mentioned earlier in the article that when we run the application through visual studio using ctrl+F5 or F5 then by default IIS express profile is selected and our application is served by the IIS Express, if we want we can easily edit these settings by directly editing the launchSettings.json file or using the Graphical user interface (GUI) provided by visual studio.

To access the GUI follow below steps.

  • Right click on the project name in Solution Explorer and select "Properties" from the context menu, it will open the project properties window
  • Click on the "Debug" tab on the project "Properties" window.
GUI for Debug profile
GUI for editing debug profiles

As you can see, using GUI we can change the default profile used by the visual studio, we can also modify the value of the Environment variable. If we edit any profile using GUI then it is immediately reflect in the launchSettings.json file and the value gets automatically updated, as GUI and launchSettings.json file are always in sync.