OpenWebConfiguration和ConfigurationManager之间的MVC5应用程序的注意事项

时间:2018-03-27 20:38:53

标签: c# asp.net asp.net-mvc-5

我需要从Web.config中读取我的ASP.NET MVC5应用程序的设置。

<configuration>
    <appSettings>
        <add key="MyKey" value="MyValue" />
    </appSettings>
</configuration>

我看到有人推荐使用OpenWebConfiguration的例子,但我通常使用ConfigurationManager.AppSettings [“MyKey”]。检查MSDN文档,OpenWebConfiguration看起来较旧,但仍然有效。我计划将我的应用程序部署到IIS服务器。 ConfigurationManager是否取代了OpenWebConfiguration,还是用于不同的任务?

谢谢你, 丹·蔡斯

1 个答案:

答案 0 :(得分:1)

OpenWebConfigurationweb.config文件的特定内容。

ConfigurationManager与配置文件混合,因此它可以从app.configweb.configmobile.config文件中读取信息。

另一个考虑因素是OpenWebConfiguration允许您轻松更改Web配置文件。 OpenWebConfiguration上有System.Web,而ConfigurationManager上有System.Configuration

使用ConfigurationManager因为它是混合的,如果您不需要更改配置文件,只需阅读已定义的配置即可。我总是在网页,表单,服务等中使用ConfigurationManager来阅读appSettings和ConnectionStrings。