ASP. NET Core environment variables and startup settings configuration tutorial, core Environment Variables

Source: Internet
Author: User

ASP. NET Core environment variables and startup settings configuration tutorial, core Environment Variables

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 ", A friendly error page is displayed.

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" } }}

Here, 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:

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" rel="external nofollow" /> <link rel="stylesheet" href="~/css/site.css" rel="external nofollow" /> </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" rel="external nofollow"   asp-fallback-test-class="sr-only" asp-fallback-test-property="position" asp-fallback-test-value="absolute" /> <link rel="stylesheet" href="~/css/site.min.css" rel="external nofollow" 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.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.