What is the recommended way of allowing an ASP.NET application to know which environment it runs in? My application can run in these environments:
In each of these environments the application needs some different configuration. The main difference between the environments is which database connection settings to use, but there are also other differences.
This is what I have tried:
So, how is the recommended way of doing this in ASP.NET? In Ruby on Rails I would use the RAILS_ENV environment variable, and in PHP I would include a file, which is not checked into the repository and is added to the ignore list of the version control system.
I would have separate web.config files for each of your environments.
Alternatively, You could also have something set up similar to what you did with your PHP include file by adding an external config file.
Just add the following to your web.config.
<appSettings file="MySettings.config">
Then add a new file called MySettings.config that contains the rest of your config information.
The web.config file is used for environmental configurations. If you find configuration in visual studios to be too cumbersome you can just keep the file out of version control, and edit it for each environment. in the <appSettings> node you can add keys:
<appSettings>
<add key="Environment" value="DEV" />
</appSettings>
Then you can access these values via the ConfigurationManager class
var currentEnvironment = ConfigurationManager.AppSettings["Environment"].ToString();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.