In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the knowledge of "how to use multiple environments in ASP.NET Core". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
ASP.NET Core supports managing applications in multiple environments, such as development (Development), rehearsal (Staging), and production (Production). Environment variables are used to indicate the environment in which the application is running, allowing the application to be configured appropriately.
1. Environment variable
ASP.NET Core refers to a specific environment variable ASPNETCORE_ENVIRONMENT to describe the environment in which the application is currently running. This variable can be set to any value, but three values are agreed to be used: Development (development), Staging (rehearsal), and Production (production).
The current environment variable settings can be detected programmatically from the application. You can also use Environment tag helper in view to include certain parts based on the current application environment. The specified environment variable name is not case-sensitive.
Development
Development environment. You can set it in vs, right-click the project, and click Properties:
When you modify the default settings created by the project, the launchSettings.json file is changed. This file contains specific settings for each configuration file in vs to start the application, including any environment variables that should be used.
You can also configure multiple different startup configurations for your application.
Staging
The Staging environment is a pre-production environment for final testing before deployment to a production environment. In theory, its physical characteristics should be a true portrayal of the production environment.
Production
The Production environment is the environment in which applications run. This environment should be configured to maximize security, performance, and application robustness. Additional settings are required:
Start caching
Client resource packaging
Use the official error page
Enable production logging and monitorin
Etc.
two。 Runtime determines the environment
IHostingEnvironment services provide the core abstraction for the work environment. This service is provided by the ASP.NET host layer and can be injected into the startup class through DI. Use this method to load a specific environment configuration file (if any) in VS's ASP.NET Core site template, and you can set different configurations depending on the environment.
Public void Configure (IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) {if (env.IsDevelopment ()) {app.UseDeveloperExceptionPage (); app.UseDatabaseErrorPage (); app.UseBrowserLink ();} else {app.UseExceptionHandler ("/ Home/Error") App.UseHsts ();} app.UseMvc (routes = > {routes.MapRoute (name: "default", template: "{controller=Home} / {action=Index} / {id?}");});}
In addition to using env.IsDevelopment (), you can also use env.IsEnvironment ("Development") or env.EnvironmentName = = "Development" to check the running environment.
In the development environment, using the UseDeveloperExceptionPage (), UseBrowserLink () methods to enable the error page in the development environment and enabling the BrowserLink function in Visual Stuido, these 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 features, but point the error page to the path "/ Home/Error" to show the user a friendly error interface.
When running the application in development mode, we use local Bootstrap files and custom css files; but if we are running in a preview and production environment, we use file copies and compressed custom styles on the ASP.NET content distribution network (CDN). We can use the environment tag to match the environment:
3. Activation convention
ASP.NET Core supports a convention-based approach to configuring the startup of an application based on the current environment.
We know that after ASP.NET Core starts, the Startup class responds to the application. If the class is named Startup {EnvironmentName}, it will be used when it is started in the corresponding environment.
This convention is also supported by the Configure and ConfigureServices methods in the Startup class.
That's all for the content of "how to use multiple environments in ASP.NET Core". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.