如何打开web.config

时间:2010-07-14 08:03:05

标签: asp.net asp.net-mvc

我正在使用以下代码打开当前Web应用程序的web.config。

Configuration rootConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/MyAppRoot");

但它只适用于我的开发机器,而不适用于生产机器。我应该使用“/ MyAppRoot”以外的其他东西吗?

提前致谢!

3 个答案:

答案 0 :(得分:1)

感谢您的回答,马修!对此,我真的非常感激!但我终于发现问题发生是因为我的应用程序没有在默认站点名下运行。通过将httpContext.Request.ApplicationPath而不是“/ MyAppRoot”传递给OpenWebConfiguration方法来解决该问题。

答案 1 :(得分:0)

您的生产服务器很可能在Medium Trust中运行。 OpenWebConfiguration可能会失败,因为它还需要加载Machine.config和主Web.config文件。

你想要访问什么?您可能应该使用ConfigurationSection类以外的任何内容限制访问配置文件。

答案 2 :(得分:0)

MSDN documentation on this methodpath参数是

The virtual path to the configuration file. If a null reference (Nothing in Visual Basic), the root Web.config file is opened.

在那里传递null,它将打开你的web.config文件。

相关问题