Configure environment variables and startup settings in ASP. NET Core, asp. netcore
In this section, we will discuss a new feature in ASP. NET Core: environment variables and startup settings, which makes debugging and testing easier during development. You only need to modify the configuration file to switch the development, rehearsal, and production environments.
ASPNETCORE_ENVIRONMENT
The Core of ASP. NET Core control environment switching is the "ASPNETCORE_ENVIRONMENT" environment variable, which directly controls the environment type of the current application program. You can modify the environment variable by right-clicking the "properties" option in the project and switching to the "debug" tab.
The environment variable framework provides three values by default. Of course, you can also define other values:
- Development (Development)
- Staging)
- Production)
In the Startup. cs file, we can use corresponding methods to control the behavior of the application. The following is the default code generated by the Startup. cs file when the sample program is created:
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { loggerFactory.AddConsole(Configuration.GetSection("Logging")); loggerFactory.AddDebug(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseBrowserLink(); } else { app.UseExceptionHandler("/Home/Error"); } app.UseStaticFiles(); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); }
The IHostingEnvironment type variables indicate the environment where the current application is running. ASP. Net Core provides four extension methods to detect the current value of "ASPNETCORE_ENVIRONMENT.
- IsDevelopment ()
- IsStaging ()
- IsProduction ()
- IsEnvironment ()
If you need to check whether the application is running in a specific environment, you can use env. isEnvironment ("environmentname"). This method is case-insensitive (please do not use env. environmentName = "Development" to check the environment ).
We can see from the code above that if the current development environment is used, use the usemediaexceptionpage () and UseBrowserLink () methods to enable the error page of the development environment and the Browser Link function in Visual Stuido, these functions help us to debug the program during the development process. However, we do not want to enable these functions in the production environment, but direct the Error page to the path "/Home/Error ", displays friendly error messages to users.
LaunchSettings. json File
ASP. Net Core contains a new launchSettings. json file. You can find this file in the "Properties" folder of the project:
This file sets different environments that Visual Studio can start. The following is the default code generated by the launchSettings. json file in the example project:
{ "iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { "applicationUrl": "http://localhost:22437/", "sslPort": 0 } }, "profiles": { "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }, "CoreWebApp": { "commandName": "Project", "launchBrowser": true, "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" }, "applicationUrl": "http://localhost:22438" } }}
There are two configuration nodes: "IIS Express" and "CoreWebApp", which correspond to the drop-down options of the start debugging button of Visual Stuido respectively, you can select the corresponding options to start the application:
The launchSettings. json file is used to set the environment in which Visual Stuido runs the application. You can also add a node. The node name is automatically added to the drop-down menu of the Visual Stuido debug button.
Now let's take a closer look at the details of these attributes:
{"IisSettings": {"windowsAuthentication": false, // enable Windows Authentication "anonymousAuthentication": true, // enable Anonymous Authentication "iisExpress": {"applicationUrl ": "http: // localhost: 22437/", // Url of the application startup. "SslPort": 44355 // SSL-enabled port }}, "profiles": {"IIS Express": {"commandName": "IISExpress", "commandLineArgs ":"", // The parameter "workingDirectory": "", // set the command's working directory "launchBrowser": true, // whether to enable "launchUrl" in the browser ": "1111", // The relative URL started in the browser "environmentVariables": {// set the environment variable to a key/value pair "ASPNETCORE_ENVIRONMENT": "Development "}}}}
For more information about other properties, go to this link: http://json.schemastore.org/launchsettings.
Environment label
With this label, the application modifies the structure of the MVC view based on the current running environment. The default code generated by the _ Layout. cshtml file in the example project:
<environment names="Development"> <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.css" /> <link rel="stylesheet" href="~/css/site.css" /> </environment> <environment names="Staging,Production"> <link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.7/css/bootstrap.min.css" asp-fallback-href="~/lib/bootstrap/dist/css/bootstrap.min.css" asp-fallback-test-class="sr-only" asp-fallback-test-property="position" asp-fallback-test-value="absolute" /> <link rel="stylesheet" href="~/css/site.min.css" asp-append-version="true" /> </environment>
In this example, when running an application in development mode, we use a local Bootstrap file and a custom css file, we use ASP. file copies and compressed custom styles on the. NET content delivery network (CDN. In this way, we can improve the application performance.
Summary
In ASP. NET Core, developers can use environment variables to easily control the behavior of applications in different environments. With these features, we have completed the following features:
- Create and use a custom environment;
- Enable or disable some functions of an application based on the environment where the application is running;
- Use the environment label to modify the MVC view in the current environment.
Reprinted please indicate the source, original link: http://www.cnblogs.com/tdfblog/p/Environments-LaunchSettings-in-Asp-Net-Core.html