New features in ASP-environment variables and startup settings

Source: Internet
Author: User
This article is mainly for you to introduce the ASP. NET core environment variables and start-up configuration tutorial, with a certain reference value, interested in small partners can refer to

In this section, we discuss a new feature in ASP: Environment variables and startup settings, which make debugging and testing in the development process much simpler. We need to simply modify the configuration file, we can realize the development, rehearsal, production environment switch.

Aspnetcore_environment

The most important thing about the ASP. NET core Control environment switch is the "aspnetcore_environment" environment variable, which directly controls the type of environment the current application is running. You can modify this environment variable by selecting the Properties option on the project's right-click menu and then switching to the Debug tab.

This environment variable framework provides three values by default, but you can also define other values:

Development(development)
Staging(rehearsal)
Production(production)

In the Startup.cs file, we can use the appropriate method to control the behavior of the application. The following is the default code generated by the Startup.cs file when you create the sample program:


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

Variables of type ihostingenvironment represent the environment in which the current application is running, and ASP. NET core provides four extension methods for detecting the current value of "aspnetcore_environment".

Isdevelopment ()
Isstaging ()
Isproduction ()
Isenvironment ()

If you need to check if the application is running in a specific environment, you can use Env. Isenvironment ("Environmentname"), this method ignores case (do not use env.) Environmentname = = "Development" to check the environment).

Through the above code, we can know that if you are currently a development environment, use the Usedeveloperexceptionpage (), Usebrowserlink () method to enable error pages for the development environment, and enable browser in visual Stuido The link feature, which helps us debug the program during the development process, but in the production environment we do not want to enable these features, but instead of the error page point to the path "/home/error", to show the user friendly error interface.

Launchsettings.json file

ASP. NET core contains a new Launchsettings.json file that you can find in the "Properties" folder in your project:

This file sets the different environments that Visual Studio can launch, the following is the default code generated by the Launchsettings.json file in the sample project:


{"Iissettings": {"windowsauthentication": false, "anonymousauthentication": True, "iisexpress": {"ApplicationUrl": " http://localhost:22437/"," Sslport ": 0}," profiles ": {" IIS Express ": {" CommandName ":" Iisexpress "," Launchbrowser ": t Rue,  "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", "Corewebapp", two nodes corresponding to the drop-down options for the Start Debug button of the visual Stuido:

The Launchsettings.json file is used to set the environment in which the application runs in Visual Stuido. We can also add a node, which is automatically added to the drop-down options of the Visual stuido Debug button.

Now let's take a detailed look at the details of these properties:


{"Iissettings": {"windowsauthentication": false,//Enable Windows Authentication "Anonymousauthentication": true,//Enable Anonymous Authentication " Iisexpress ": {" ApplicationUrl ":" http://localhost:22437/",//URL path for app launch. "Sslport": 44355//SSL-enabled Port}}, "profiles": {"IIS Express": {"CommandName": "Iisexpress", "CommandLineArgs": "",//Pass command Parameter "workingdirectory": "",//Set the working directory of the command "Launchbrowser": true,//whether to launch "Launchurl" in the browser: "1111",//the relative URL launched in the browser " Environmentvariables ": {//SET environment variable to key/value pair" aspnetcore_environment ":" Development "}}}

For more information about additional properties, go to this link: http://json.schemastore.org/launchsettings.

Environment label

With this tag, the application modifies the structure of the MVC view based on the currently running environment. The default code generated by the _layout.cshtml file in the sample 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 you run the application in development mode, we use the local bootstrap file and the custom CSS file, but if you run in a staging and production environment, we use a copy of the file on the ASP. NET Content distribution network (CDN) and a custom style that has been compressed. In this way, we can improve the performance of the application.

Summarize

in ASP. NET core, developers can use environment variables to easily control the behavior of the application in different environments. Using these features, we complete the following functions:

    • Create and use a custom environment;

    • Enable or disable partial functionality of the application depending on the environment in which the application is running;

    • Use the environment tag to modify the MVC view in the current environment.

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.