Wednesday, February 6, 2019

What is the use of launchsettings.json file in ASP.NET Core?

Keep in mind that, this file is only required on local development machine, we do not need it for publishing our ASP.NET Core application

launchsettings.json file is located inside the properties folder in solution explorer
Fig. 1. location
  • ASP.NET Core configures app behavior based on the runtime environment using an environment variable.
Fig. 2. launch profiles in asp.net core

  • This json file holds project specific settings associated with debug profile and launch of the application, including any environment variables that should be used
  • In the figure 2, it shows that there are 2 profiles(IIS Express & with same name as of project name) used in launchsettings.json
  • By default VS2017 uses the IIS Express as shown in Figure 2
  • Now lets see the output by running the application
Fig. 3. mapping browser url with launch profile
  • Notice in Figure 3, the Url with port number 3118 is mapped with iisSettings (Setting used for profile "IIS Express") of applicationUrl properties at launchsettings.json file
  • ASP.NET Core reads the environment variable ASPNETCORE_ENVIRONMENT at app startup and stores the value in IHostingEnvironment.EnvironmentName 
  • You can set ASPNETCORE_ENVIRONMENT to any value but,  by convention there are three values supported by the framework: DevelopmentStaging, and Production as shown in Figure 4
Fig. 4. Environment Variables
  • In the Figure 4, as for e.g. env.IsDevelopment() checks either the environment variable is set to Development or not, in the launchsettings.json file, as shown in the figure 2.
  • If ASPNETCORE_ENVIRONMENT isn't set, it defaults to Production
  • Now lets change the launch profile to MyFirstDotNetCoreApplication (this name will be your project name), from top of the visual studio and run the application, as shown in Figure 5
Fig. 5. Changed the profile to MyFirstDotNetCoreApplication
  • Now lets see the output
Fig. 6. Changed Profile Output
  • Notice in Figure 6, the Url used at above figure. Now it is mapped with MyFirstDotNetCoreApplication profile & at this time .NET Core CLI is used to launch the application that will popup another window for .NET Core CLI
  • Launch profiles can also be configured using the properties window of project. For this right-click on the project file and then you will see the window as shown in Figure 7.
Fig. 7. Profile changed from properties window of project
Now, just change the profile you want and run the application.


1 comment: