ASP.NET Core, launchSettings.json and appSettings.json
Introduction
In Visual Studio, when you generate code using ASP.NET Core templates, files that manage configuration information such as launchSettings.json and appSettings.json are generated.
By utilizing these, it becomes possible to branch and describe the process according to the environment and manage the configuration information to be referenced separately, so I tried to summarize the usage and usage.
launchSettings.json
launchSettings.json is simply a launch profile. Various settings for starting a program from the debugger can be defined for each profile. Which profile is applied and started is specified when the debugger is started.
The following is an example of ASP.NET Core launchSettings.json. Profiles such as “IIS Express”, “LaunchSettingsDemo”, Profile001 “are defined.
launchSettings.json
{
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"LaunchSettingsDemo": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:5001;http://localhost:5000"
},
"Profile001": {
"commandName": "IISExpress",
"launchBrowser": true
}
}
}
The commandName for each profile specifies the web server to start.
–IISExpress –Start IIS Express. (It is set by default in Visual Studio 2019.)
–IIS –Starts IIS.
–Project –Start the Kestrel web server.
Kestrel Web Server is a cross-platform web server for ASP.NET Core.
There is an explanation on the following site. It can also be used in Linux and MacOS environments.
“launchBrowser” specifies a Boolean value, and when the debugger is started, specifies whether to start the browser with a Boolean value.
“environmentVariables” is important and allows you to set environment variables and values. Here, the execution environment is defined by setting the value of “ASPNETCORE_ENVIRONMENT”.
The value set for “ASPNETCORE_ENVIRONMENT” can be any value, but the following values are defined.
–Development –Development environment
–Staging –Staging (rehearsal before deployment to production environment) environment
–Production –Production environment
If you do not define these values, they will be treated as Staging.
Select launch profile
In Visual Studio, you can select from which profile to start from the debug run button.
For the .NET Core CLI, specify the profile name with the –launch-profile option of run.
dotnet
dotnet run --launch-profile <Profile name>
Reference to ASPNETCORE_ENVIRONMENT
The setting value of the ASPNETCORE_ENVIRONMENT environment variable can be referenced from the IHostingEnvironment.EnvironmentName property. You can also use the IHostingEnvironment extension methods IsDevelopment, IsStaging, IsProduction, IsEnvironment to describe what you want to do in your current environment.
To reference these from the ASP.NET Core controller PageModel, get an instance of IWebHostEnvironment with dependency injection via the constructor, save the reference and use it.
Although not written in this example, if you define any name in ASPNETCORE_ENVIRONMENT, you can also evaluate it with IsEnvironment (string environmentName).
Index.cshtml.cs
public class IndexModel : PageModel
{
public string Message;
private readonly IWebHostEnvironment _env;
private readonly ILogger<IndexModel> _logger;
public IndexModel(IWebHostEnvironment env, ILogger<IndexModel> logger)
{
this._env = env;
this._logger = logger;
}
public void OnGet()
{
if (this._env.IsDevelopment())
{
this.Message = "I'm Development.";
}
else if (this._env.IsStaging())
{
this.Message = "I'm Staging.";
}
else if (this._env.IsProduction())
{
this.Message = "I'm Production.";
}
else
{
this.Message = "I'm Others.";
}
}
}
Even with Razor (* .cshtml), you can of course write similar code with dependency injection and extension methods.
Index.csthml
@page
@using Microsoft.Extensions.Hosting
@using Microsoft.AspNetCore.Hosting
@inject IWebHostEnvironment HostEnvironment
@{
string Message;
if (HostEnvironment.IsDevelopment())
{
Message = "I'm Development.";
}
else if (HostEnvironment.IsStaging())
{
Message = "I'm Staging.";
}
else if (HostEnvironment.IsProduction())
{
Message = "I'm Production.";
}
else
{
Message = "I'm Others.";
}
}
In Razor (* .cshtml), the environment element allows rendering to branch according to the environment.
In the following example, it refers to jquery.js in the case of development environment, and jquery.min.js in other cases.
_Layout.cshtml
<environment include="Development">
<script src="~/lib/jquery/dist/jquery.js"></script>
</environment>
<environment exclude="Development">
<script src="~/lib/jquery/dist/jquery.min.js"></script>
</environment>
appSettings.json
appSettings.json can manage app configuration information. How to get the defined key and value is explained below.
-Getting values from the configuration file appsettings.json in ASP.NET Core 3.1 MVC
The point can change the referenced appSettings.json by the ASPNETCORE_ENVIRONMENT environment variable.
You can change the configuration information of the reference destination by formatting the file name as appSettings.% ASPNETCORE_ENVIRONMENT% .json.
- appSettings.json
- appSettings.Development.json
- appSettings.Staging.json
- appSettings.Production.json
If no file contains the corresponding environment variable, appSettings.json will be referenced.
Summary
launchSettings.json is the definition of the launch profile. You can specify which profile to use when starting the debugger. The main role of the startup profile is to define the ASPNETCORE_ENVIRONMENT environment variable.
appSettings.json is the definition of app configuration information. You can change the referenced file by defining ASPNETCORE_ENVIRONMENT.
You can manage everything with environment variables without using launchSettings.json and appSettings.json, but if you design the configuration information based on json, you can define the configuration information hierarchically and it will be easier to manage.