Home  >  Article  >  Backend Development  >  New features in ASP.NET Core--environment variables and startup settings

New features in ASP.NET Core--environment variables and startup settings

零下一度
零下一度Original
2017-07-09 11:51:113384browse

This article mainly introduces the configuration tutorial of ASP.NET Core environment variables and startup settings in detail. It has certain reference value. Interested friends can refer to it

In this part, we will discuss a new feature in ASP.NET Core: environment variables and startup settings, which will make debugging and testing during the development process easier. We only need to simply modify the configuration file to achieve switching between development, preview, and production environments.

ASPNETCORE_ENVIRONMENT

The core thing for ASP.NET Core to control environment switching is the "ASPNETCORE_ENVIRONMENT" environment variable, which directly controls the type of environment in which the current application is running. You can modify this environment variable by selecting the "Properties" option from the right-click menu on the project and then switching to the "Debug" tab.

This environment variable framework provides three values ​​by default. Of course, you can also define other values:

Development(Development )
Staging(Preview)
Production(Production)

We can use the corresponding method in the Startup.cs file. Control application behavior. The following is the default code generated by the Startup.cs file when creating 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?}");
  });
 }

The variable of type IHostingEnvironment represents the environment in which the current application is running, ASP.Net Core Four extension methods are provided for detecting the current value of "ASPNETCORE_ENVIRONMENT".

IsDevelopment()
IsStaging()
IsProduction()
IsEnvironment()

If you need to check this Whether the application is running in a specific environment, you can use env.IsEnvironment("environmentname"), which ignores case (please do not use env.EnvironmentName == "Development" to check the environment).

Through the above code, we can know that if it is currently a development environment, use the UseDeveloperExceptionPage() and UseBrowserLink() methods to enable the error page of the development environment and enable the Browser Link function in Visual Stuido. These All functions are helpful for us to debug the program during the development process; but in the production environment, we do not want to enable these functions, but point the error page to the path "/Home/Error" to display a friendly error interface to the user.

launchSettings.json file

ASP.Net Core includes a new file launchSettings.json, which you can find in the "Properties" folder in your project:

This file sets up 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": 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". These two nodes correspond to the drop-down options of the Visual Stuido start debugging button:

## The #launchSettings.json file is used to set the environment for running applications in Visual Stuido. We can also add a node and the node name will automatically be added to the drop down selection of the Visual Stuido debug button.

Now let’s talk about the details of these properties in detail:


{
 "iisSettings": {
 "windowsAuthentication": false,//启用Windows身份验证
 "anonymousAuthentication": true,//启用匿名身份验证
 "iisExpress": {
 "applicationUrl": "http://localhost:22437/",//应用启动的Url路径。
 "sslPort": 44355//启用SSL的端口
 }
 },
 "profiles": {
 "IIS Express": {
 "commandName": "IISExpress",
 "commandLineArgs": "", //传递命令的参数
 "workingDirectory": "", //设置命令的工作目录
 "launchBrowser": true, //是否在浏览器中启动
 "launchUrl": "1111", //在浏览器中启动的相对URL
 "environmentVariables": { //将环境变量设置为键/值对
 "ASPNETCORE_ENVIRONMENT": "Development"
 }
 }
 }
}

To get the details of other more properties, please go to this link: http://json.schemastore.org/launchsettings.

Environment tag

Through this tag, the application modifies the structure of the MVC view according to the current 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 running the application in development mode, we use the local Bootstrap files and custom css files; but if running in staging and production environments, we use a copy of the files and compressed custom styles on the ASP.NET Content Delivery Network (CDN). In this way we can improve the performance of our application.

Summary

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

  • Create and use custom environments;

  • Enable or disable applications based on the environment in which they are running Some functions of the program;

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

The above is the detailed content of New features in ASP.NET Core--environment variables and startup settings. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn