如何配置ASP.NET应用程序的环境?
允许 ASP.NET 应用程序了解其运行环境的推荐方法是什么?我的应用程序可以在以下环境中运行:
- 在 Visual Studio 中进行调试
- 自动化系统测试
- 在登台服务器上
- 部署 在生产服务器上部署
在每种环境中,应用程序都需要一些不同的配置。环境之间的主要区别在于使用哪些数据库连接设置,但也存在其他差异。
这是我尝试过的:
- 我尝试使用 Visual Studio 中的“配置”选项。默认有两个:Release 和 Debug。我尝试用我的四个环境的标识符替换这两个。然后可以使用这些配置对 Web.config 文件应用转换。这里的问题是选择配置发生在构建时,我想编译我的应用程序,然后在 Web 服务器上运行它之前选择环境。在 Visual Studio 内调试时也不会发生 Web.config 转换,因此它不会告诉应用程序它在调试环境中运行,而是不会获得任何环境。我在 使用 Visual Studio web.config 转换进行调试 但似乎效果不太好。我必须更改项目属性中的配置,这是一个缓慢的过程。
- 我尝试过使用环境变量。这样我就可以从应用程序外部设置变量 MYAPP_ENV=dev,并且可以在 C# 中将其读取为
字符串 env = System.Environment.GetEnvironmentVariable("MYAPP_ENV");
但我不确定如何从 Visual Studio 和 IIS 中设置此环境变量。我无法在计算机上全局设置它,因为同一台计算机需要能够在多个环境中运行应用程序。
那么,在 ASP.NET 中推荐的执行此操作的方法是怎样的呢?在 Ruby on Rails 中,我将使用 RAILS_ENV 环境变量,在 PHP 中,我将包含一个文件,该文件不会签入存储库,而是会添加到版本控制系统的忽略列表中。
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:
- Debugging within Visual Studio
- An automated system test
- Deployment on a staging server
- Deployment on a production server
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:
- I have tried using the "configuration" option in Visual Studio. By default there is two: Release and Debug. I have tried to replace these two with identifiers for my four environments. These configurations can then be used to apply a transform to the Web.config file. The problem here is that choosing the configuration happens on build time, and I would like to compile my application and then choose the environment before running it on a web server. The Web.config transformation also doesn't happen when debugging inside Visual Studio, so instead of telling the application that it runs in the debugging environment, it gets no environment. I found a workaround at Use Visual Studio web.config transform for debugging but it doesn't seem to work well. I have to change the configuration in the project properties, which is a slow process.
- I have tried using an environment variable. This way I could set a variable MYAPP_ENV=dev from outside the application, and I could read it in C# as
string env = System.Environment.GetEnvironmentVariable("MYAPP_ENV");
But then I am not sure how to set this environment variable from within Visual Studio and from within IIS. I cannot set it globally on the machine because the same machine needs to be able to run the application in multiple environments.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将为您的每个环境提供单独的 web.config 文件。
或者,您也可以通过添加外部配置文件来进行类似于对 PHP 包含文件所做的设置。
只需将以下内容添加到您的 web.config 中即可。
然后添加一个名为 MySettings.config 的新文件,其中包含其余的配置信息。
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.
Then add a new file called MySettings.config that contains the rest of your config information.
web.config 文件用于环境配置。如果您发现 Visual Studio 中的配置太麻烦,您可以将文件置于版本控制之外,然后针对每个环境进行编辑。在<应用程序设置>中您可以添加键的节点:
然后您可以通过 ConfigurationManager 类访问这些值
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:
Then you can access these values via the ConfigurationManager class