Home Backend Development C#.Net Tutorial New features in ASP.NET Core--environment variables and startup settings

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

Jul 09, 2017 am 11:51 AM
asp.net core new function

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?}");
  });
 }
Copy after login

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"
 }
 }
}
Copy after login

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"
 }
 }
 }
}
Copy after login

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>
Copy after login
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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to enable Core Isolation's memory integrity feature in Windows 11 How to enable Core Isolation's memory integrity feature in Windows 11 May 10, 2023 pm 11:49 PM

Microsoft's Windows 11 2022 Update (22H2) enables CoreIsolation's memory integrity protection by default. However, if you are running an older version of the operating system, such as Windows 11 2022 Update (22H1), you will need to turn this feature on manually. Turn on CoreIsolation's Memory Integrity feature in Windows 11 For users who don't know about Core Isolation, it's a security process designed to protect basic core activities on Windows from malicious programs by isolating them in memory. This process, combined with the memory integrity feature, ensures

iPadOS18 new features iPadOS18 new features Jun 12, 2024 pm 10:41 PM

Early this morning, Apple officially released iPadOS18. This system not only has the classic functions of iOS18, but also adds some unique functions, such as supporting mathematical note calculators, etc., which further improves the experience of iPad users. Friends who are interested Come and take a look. This time iPadOS18 not only perfectly inherits the core functions of iOS18, such as the personalized control center design, which allows users to freely adjust the order and layout of control items according to personal preferences, and the highly anticipated game mode, providing gamers with smoother and more The immersive gaming experience also incorporates a number of unique features specifically targeting the iPad’s large screen advantages and the creative uses of Apple Pencil, further expanding the iPad’s productivity.

What does computer core mean? What does computer core mean? Sep 05, 2022 am 11:24 AM

Core has two meanings in computers: 1. The core, also known as the core, is the most important component of the CPU. All calculations, accepting storage commands, and processing data of the CPU are performed by the core; 2. Core, core is Intel's processor Name, Core is the processor brand launched by Intel after the Pentium processor. It has currently released twelfth generation Core processors.

Microsoft Edge browser's new feature 'Super Drag” is now available! Microsoft Edge browser's new feature 'Super Drag” is now available! Mar 04, 2024 pm 06:31 PM

The editor recently learned that the new feature of Microsoft Edge browser "Super Drag" has been launched, unlocking the fourth way to open links in new tabs, making it easier for users to open links faster. Currently, in the Microsoft Edge browser, if users want to open a link or image in a new tab, there are three ways: 1. Right-click the link or image, and then select the corresponding operation option. 2. Drag the link or image to the tab bar. 3. Use the mouse wheel to click on the link or image. "Super Drag" brings a fourth kind of interaction, where users click on a link, part of text, or image and then drag it sideways, up, or down a little to open it in a new tab. After the user drags the text, the default search engine of the Edge browser will be called by default and a new tab will be opened.

WeChat launches new feature: 'Quiet Mode” WeChat launches new feature: 'Quiet Mode” Mar 04, 2024 pm 01:43 PM

It is understood that WeChat has launched a new feature: "Quiet Mode". After turning it on, WeChat will no longer make sounds in all scenarios such as message notifications, audio and video calls, and video account content playback. Update to the latest version of WeChat on iOS and turn it on through "WeChat" - "Me" - "Settings" - "Care Mode" - "Quiet Mode". After that, WeChat will no longer make sounds in all scenarios such as message notifications, audio and video calls, and video account content playback. WeChat said the feature was developed to meet the needs of those who cannot hear. For most people, sound may be very common and accessible, but they often ignore that the deaf community does not have this perception ability, although they also use mobile phones and WeChat like everyone else. according to

How to Fix Processor Thermal Trip Error in Windows 11/10 [Fix] How to Fix Processor Thermal Trip Error in Windows 11/10 [Fix] Apr 17, 2023 am 08:13 AM

Most of the devices, such as laptops and desktops, have been heavily used by young gamers and coders for a long time. The system sometimes hangs due to application overload. This forces users to shut down their systems. This mainly happens to players who install and play heavy games. When the system tries to boot after force shutdown, it throws an error on a black screen as shown below: Below are the warnings detected during this boot. These can be viewed in the settings on the event log page. Warning: Processor thermal trip. Press any key to continue. ..These types of warning messages are always thrown when the processor temperature of a desktop or laptop exceeds its threshold temperature. Listed below are the reasons why this happens on Windows systems. Many heavy applications are in

iOS18 new features revealed iOS18 new features revealed Mar 06, 2024 pm 02:25 PM

As Apple is about to release iOS 18, its major annual software update, this will undoubtedly become one of the hot and highly anticipated releases in 2024. According to rumors, this update will be regarded as one of the largest software upgrades in the history of the iPhone. Here is a brief summary of the relevant function upgrades. New iOS 18 feature revelations 1. It is reported that Apple may integrate the visionOS style into iOS 18. , giving people a refreshing operating experience. 2. According to rumors, the company is actively testing AI models and is expected to make major improvements to Siri on the iOS18 version. 3. In addition, Apple has recently launched a pilot program and launched a new tool called "Ask" to AppleCare, aiming to improve the efficiency of customer inquiries. 4

Windows 11 details new features Windows 11 details new features Dec 23, 2023 pm 03:37 PM

The new win11 operating system has brought many new functions and features to users, which are very novel, easy to use and powerful. Today we will bring you a detailed introduction to the new features of win11. If you are not sure, come here for details. Let’s find out. What are the new features of win11: 1. The new design of Win11 makes the UI look simple and beautiful, and more modern. 2. Microsoft said that the start menu and application icons in the center of the taskbar can allow users to find what they need more quickly. 3. The update package of Win11 is 40% less than that of Win10 on average, which means it saves more power and runs faster. 4. The speed of the built-in Edge browser has also been improved. On Win11, multiple task arrangement methods have been added.

See all articles