2010-07-14 88 views
0

我正在使用以下代碼打開當前Web應用程序的web.config。如何打開web.config

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

但它只適用於我的開發機器,而不是生產機器。我應該使用「/ MyAppRoot」以外的其他東西嗎?

在此先感謝!

+0

你爲什麼不使用ConfigurationManager? – 2010-07-14 19:58:28

回答

1

謝謝你的回答,馬修!對此,我真的非常感激!但我終於發現問題發生,因爲我的應用程序沒有運行在默認站點名稱下。通過將httpContext.Request.ApplicationPath而不是「/ MyAppRoot」傳遞給OpenWebConfiguration方法解決了問題。

0

您的生產服務器可能在Medium Trust中運行。 OpenWebConfiguration可能會失敗,因爲它需要在Machine.config和主Web.config文件中加載。

你想要訪問什麼?您應該限制訪問配置文件,而不是使用ConfigurationSection類。

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文件。