- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Explain the ASP.NET Core support for multiple environments for development and production
Running an application in production for live customers is very different from running it when you are developing it on your local machine. In production, your application is hosted on a server which has very different configurations and specifications than your computer. Various services that your application talks to, such as databases or external APIs change for production.
By letting the application know which environment it’s running, you can vary the application’s behavior. ASP.NET Core makes it easy to manage various environments effortlessly. You can configure different configuration settings for different environments, and tweak them without having to recompile the application. This lets you easily change the environment.
There are three parts to managing environments for your application, identifying the environment, loading different configuration settings based on the environment, and changing the environment.
To determine the runtime environment, ASP.NET Core uses environment variables named DOTNET_ENVIRONMENT or ASPNETCORE_ENVIRONMENT when the ConfigureWebHostDefaults method is called. The second environment variable overrides the first one.
If the application doesn’t find an ASPNETCORE_ENVIRONMENT variable when it starts, it defaults to a production environment. That means, unless you set up the variable on the deployment machine explicitly to development, all ASP.NET Core applications start in the production mode.
You can also use the launchSettings.json file to set up the hosting environment. This file resides in the Properties folder, and defines profiles for running the application. Here’s an example of a typical launchSettings.json file.
{ "iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { "applicationUrl": "http://localhost:17456", "sslPort": 44394 } }, "profiles": { "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }, "TutorialsPoint": { "commandName": "Project", "dotnetRunMessages": "true", "launchBrowser": true, "applicationUrl": "https://localhost:5001;http://localhost:5000", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } } } }
Since an ASP.NET Core application starts as a console application, the current hosting environment is logged to the console upon startup.
➜ TutorialsPoint dotnet run Building... info: Microsoft.Hosting.Lifetime[0] Now listening on: https://localhost:5001 info: Microsoft.Hosting.Lifetime[0] Now listening on: http://localhost:5000 info: Microsoft.Hosting.Lifetime[0] Application started. Press Ctrl+C to shut down. info: Microsoft.Hosting.Lifetime[0] Hosting environment: Development info: Microsoft.Hosting.Lifetime[0] Content root path: /TutorialsPoint
The IWebHostEnvironment parameter passed to the Configure() method in the Startup class provides helper methods named IsDevelopment(), IsStaging(), etc. to easily identify the environment. It also has a method named IsEnvironment(envName) that takes a string and returns if the environment name matches that string. You can use it to identify any custom environments.
Though you can use any value to specify an environment, out-of-the-box ASP.NET Core provides three values:
- Development
- Staging
- Production
public void Configure(IApplicationBuilder app, IWebHostEnvironment env){ if (env.IsDevelopment()){ app.UseDeveloperExceptionPage(); } if (env.IsProduction() || env.IsStaging() || env.IsEnvironment("Staging_2")){ app.UseExceptionHandler("/Error"); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints =>{ endpoints.MapRazorPages(); }); }
ASP.NET Core determines the environment early in the startup process, even before the configuration is set up. This allows you to dynamically change the configuration providers based on the environment.
- Related Articles
- Explain how logging works in ASP.NET Core
- Explain the role of HttpContext class in ASP.NET Core
- Explain the HTTP request-response lifecycle in ASP.NET Core.
- Explain how Razor Pages work in ASP.NET Core
- Explain how error handling works in ASP.NET Core
- Explain the purpose of the Program class in ASP.NET Core
- Explain the purpose of the Startup class in ASP.NET Core
- What is ASP.NET Core? Explain how it's different from the ASP.NET framework.
- Explain how to create a new ASP.NET Core project
- Explain how static files are served in ASP.NET Core
- Explain- Food provides raw materials for growth and development.
- What is routing? Explain how it works in ASP.NET Core
- What are the benefits of choosing ASP.NET Core over ASP.NET?
- What is Kestral C# Asp.net Core?
- How to specify service lifetime for register service that added as a dependency C# Asp.net Core?
