Loading

ASP.NET Core

How to configure launchSettings.json file in ASP.NET Core?. The Complete ASP.NET Core Developer Course 2023 [Videos].

The launchSettings. json file is used to store the configuration information, which describes how to start the ASP.NET Core application, using Visual Studio. The file is used only during the development of the application using Visual Studio. It contains only those settings that required to run the application.

ASP.NET Core launchSettings.json file

In this Video, I am going to discuss the use and importance of the ASP.NET Core launchSettings.json file in detail. Please read our previous Video where we discussed the OuOfProcess Hosting Model in ASP.NET Core Web Application. In order to understand the ASP.NET Core launchSettings.json file, let us first create a new ASP.NET Core application with an empty template.

Creating a new Empty ASP.NET Core Web Application

First, open Visual Studio 2019 and then click on the “Create a new project” box as shown in the below image.

Creating a new Empty ASP.NET Core Web Application

Once you click on the Create a new project box, then it will open the Create a new project window. From this window, you need to select the ASP.NET Core Web Application template and then click on the Next button as shown in the below image.

ASP.NET Core Web Application template

Once you click on the Next button, then it will open the “Configure Your New Project” window. Here, you need to give the name for your project, set the location of your project, give the solution name. In this example, I will give the name “FirstCoreWebApplication” and then click on the Create button as shown in the image below.

Configure Your New Project

Once you click on the create button, then it will open the Create a new ASP.NET Core Web Application window. From this window, you need to select the Empty Project template and uncheck all the checkboxes from the Advanced section. Please make sure to select .NET Core and ASP.NET Core 3.1 from the respective dropdown list and finally click on the Create button as shown in the below image.

Create a new ASP.NET Core Web Application

Once you click on the Create button, it will take some time and will create the Empty ASP.NET Core Web Application with the following file and folder structure.

Empty ASP.NET Core Web Application

As you can see from the above image, the ASP.NET Core Project has a file called launchSettings.json within the Properties folder. So, let us discuss the need and importance of this launchSettings.json file in the ASP.NET Core application.

Understanding LaunchSettings.json file in ASP.NET Core

The settings that are present within this file are going to be used when we run the .NET core application either from Visual Studio or by using .NET Core CLI.

The most important point that you need to keep in mind is this launchSettings.json file is only used within the local development machine. That means this file is not required when we publishing our asp.net core application to the production server.

If you have certain settings and you want your application to use such settings when you publish and deploy your asp.net core application to the production server, then you need to store such settings in the appsettings.json file. Generally, in the ASP.NET Core application, the configuration settings are going to be stored in the appsettings.json file. In our upcoming Video, we will discuss the appsettings.json file in detail.

ASP.NET Core Application Profile settings in the launchSettings.json file:

If you open the launchSettings.json file, then by default you will find the following code or you can say settings within that file in ASP.NET Core 3.1 applications.

{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:50409",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"FirstCoreWebApplication": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

In the above launchSettings.json file, within the profiles, we have two sections i.e. IIS Express and FirstCoreWebApplication as shown in the below image.

ASP.NET Core Application Profile settings in the launchSettings.json file

The point that you need to remember is when you run the application from Visual Studio either by pressing CTRL + F5 or just F5 then by default the profile with “commandName”: “IISExpress” is going to be used. On the other hand, if you run the ASP.NET Core application using .NET Core CLI (i.e. dotnet run command), then the profile with the “commandName”: “Project” is going to be used. 

However, if you want then you can choose which profile to use when you run the application by pressing CTRL + F5 or just F5, by clicking on the drop-down list in Visual Studio as shown below

ASP.NET Core launchSettings.json file

The value of the commandName property of the launchSettings.json file can be any one of the following.  

  1. IISExpress
  2. IIS
  3. Project

The CommandName property value of the launchSettings.json file along with the AspNetCoreHostingModel element value from the applications project file will determine the internal and external web server (reverse proxy server) that are going to use and handle the incoming HTTP Requests. For better understanding, please have a look at the below table.

Internal and External Web Server in ASP.NET Core

Modifying the Configure method of Startup class

Modify the Configure method of the Startup class file as shown below to display the Name of the worker process that is going to handle the request in the browser window.

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Worker Process Name : " +
System.Diagnostics.Process.GetCurrentProcess().ProcessName);
});
});
}
Case1: CommandName as Project

When we use the CommandName as Project in the launchSettings.json file, then ASP.NET Core is going to ignore the AspNetCoreHostingModel value. The Kestrel is the only server that is going to host the application and handle the incoming request. Lets prove this. Now, we need to set the launch Profile as FirstCoreWebApplication as shown below.

CommandName as Project

If you look at the launchSettings.json file, then you will see that the FirstCoreWebApplication profile uses the “commandName”: “Project” value, and as well as please focus on the application URL as shown below. In my application the URL is http://localhost:5000 and the port number might be varying in your example.

launchSettings.json file in ASP.NET Core

Now change the AspNetCoreHostingModel element value to InProcess in the applications project file as shown below.

<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
</PropertyGroup>
</Project>

Now, if you run the project either by pressing CTRL + F5 or just F5, first it will launch the command prompt where it will host the application using the Kestrel server as shown below.

host the application using the Kestrel server

Once it launches the command prompt and hosts the application, then by default it opens your default browser and displays the worker process name as the project name i.e. FirstCoreWebApplication as shown in the below image. This is because when the CommandName value is Project then it ignores the AspNetCoreHostingModel value and Kestrel is the only server that is going to host and process the incoming requests.

run the application using the Kestrel server

Case2: CommandName as IISExpress and AspNetCoreHostingModel as InProcess

If we use the CommandName as IISExpress profile and if we set the AspNetCoreHostingModel value as InProcess in the applications project file, then IIS Express is the only server that is going to host and handle the incoming HTTP request. Let us prove this. First, use IIS Express as the lunch profile by selecting IIS Express from the drop-down list as shown below.

CommandName as IISExpress and AspNetCoreHostingModel as InProcess

Now, if you look at the launchSettings.json file, then you will see that the IIS Express profile use the “commandName”: “IISExpress” value and also please keep the focus on the application URL which is coming from the iisSettings section as shown below

CommandName as IISExpress and AspNetCoreHostingModel as InProcess

We already set the AspNetCoreHostingModel element value to InProcess. Now, when we run the application either by pressing CTRL + F5 or just F5, then it will display the value as iisexpress for the worker process name as shown in the below image and also have a look at the browser URL. This proves that IIS Express is the only web server that is going to host the application as well as handles the incoming HTTP Requests.

ASP.NET Core launchSettings.json file

Case3: CommandName as IISExpress and AspNetCoreHostingModel as OutOfProcess

If we use the CommandName as IISExpress profile and if we set the AspNetCoreHostingModel value as OutOfProcess then ASP.NET Core uses IIS Express as the external web server and Kestrel is the internal webserver. The external web server i.e. IIS Express will receive the incoming HTTP Requests and then forward the request to the internal web server i.e. Kestrel which is going to process the request. Let us prove this.

As we already set the launch profile as IIS Express, we just need to change the AspNetCoreHostingModel element value to OutOfProcess in the applications project file as shown below.

<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<AspNetCoreHostingModel>OutOfProcess</AspNetCoreHostingModel>
</PropertyGroup>
</Project>

Thats it. Run the application and it should display the project name as the worker process name as shown in the below image as the request is internally handled by Kestrel Web Server.

CommandName as IISExpress and AspNetCoreHostingModel as OutOfProcess

The rest two cases we will discuss in a later Video when we host the application using IIS. If you want then you can also change the settings of the launchSettings.json file using the Graphical User Interface (GUI) provided by Visual Studio.

How to access the Graphical User Interface (GUI) in Visual Studio?

To do so, right-click on the project name in Solution Explorer and then select the “Properties” option from the context menu. Once you open the project properties window, click on the “Debug” tab on the as shown in the below image.

How to access the Graphical User Interface (GUI) in Visual Studio?

Using the Graphical User Interface, you can also change the settings of the launchSettings.json file. Now here you can see that the Environment Variable “ASPNETCORE_ENVIRONMENT” is set to “Development”. You can change this Environment Variable value to Staging or Production depending on where you are running your application.

If you want, then you can also add new environment Variables. These environment variables are available throughout your application. And if you want then you can also execute some code conditionally depending on the environment variables value. For example, consider the following Configure() method of Startup.cs file.

ASP.NET Core launchSettings.json

It checks if the environment is Development, then it is going to display the Developer Exception Page. In our upcoming Videos, we are going to discuss more these environment variables. 

See All

Comments (727 Comments)

Submit Your Comment

See All Posts

Related Posts

ASP.NET Core / Blog

What is ASP.NET Core?

ASP.NET Core is the new version of the ASP.NET web framework mainly targeted to run on .NET Core platform.
27-jan-2022 /16 /727

ASP.NET Core / Blog

What is ASP.NET Core Framework?

ASP.NET Core is a cross-platform, high-performance, open-source framework for building modern, cloud-enabled, Internet-connected apps. With ASP.NET Core, you can: Build web apps and services, Internet of Things (IoT) apps, and mobile backends. Use your favorite development tools on Windows, macOS, and Linux.
27-jan-2022 /16 /727

ASP.NET Core / Blog

How to Setup ASP.NET Core Environment ?

ASP.NET Core is a cross-platform, high-performance, open-source framework for building modern, cloud-enabled, Internet-connected apps. With ASP.NET Core, you can: Build web apps and services, Internet of Things (IoT) apps, and mobile backends. Use your favorite development tools on Windows, macOS, and Linux.
27-jan-2022 /16 /727