我需要从Web.config中读取我的ASP.NET MVC5应用程序的设置。
<configuration>
<appSettings>
<add key="MyKey" value="MyValue" />
</appSettings>
</configuration>
我看到有人推荐使用OpenWebConfiguration的例子,但我通常使用ConfigurationManager.AppSettings [“MyKey”]。检查MSDN文档,OpenWebConfiguration看起来较旧,但仍然有效。我计划将我的应用程序部署到IIS服务器。 ConfigurationManager是否取代了OpenWebConfiguration,还是用于不同的任务?
谢谢你, 丹·蔡斯
答案 0 :(得分:1)
OpenWebConfiguration
是web.config
文件的特定内容。
ConfigurationManager
与配置文件混合,因此它可以从app.config
,web.config
和mobile.config
文件中读取信息。
另一个考虑因素是OpenWebConfiguration
允许您轻松更改Web配置文件。 OpenWebConfiguration
上有System.Web
,而ConfigurationManager
上有System.Configuration
。
使用ConfigurationManager
因为它是混合的,如果您不需要更改配置文件,只需阅读已定义的配置即可。我总是在网页,表单,服务等中使用ConfigurationManager
来阅读appSettings和ConnectionStrings。