簡體   English   中英

WebConfigurationManager和ConfigurationManager之間有什么區別?

[英]What's the difference between the WebConfigurationManager and the ConfigurationManager?

WebConfigurationManagerConfigurationManager之間有什么區別?

我什么時候應該使用另一個?

更新

我只是查看了WebConfigurationManager ,由於某種原因,您無法像在ConfigurationManager那樣訪問連接字符串(如數組)。 誰能告訴我為什么MS這樣做了? 使用WebConfigurationManager獲取所需的連接字符串似乎很痛苦。

再次更新CAVEAT!

如果您沒有對添加到項目中的System.Configuration命名空間的引用,那么當您嘗試訪問WebConfigurationManager.ConnectionStrings時,Visual Studio將顯示錯誤!

WebConfigurationManger知道如何在Web應用程序中處理配置繼承。 如您所知,在一個應用程序中可能有多個web.config文件 - 一個位於站點的根目錄中,任何數字位於子目錄中。 您可以將路徑傳遞給GetSection()方法以獲取可能的重寫配置。

如果我們使用Reflector在WebConfigurationManager上閑逛,那么事情很清楚:

public static object GetSection(string sectionName)
{
    ...
    return ConfigurationManager.GetSection(sectionName);
}

public static object GetSection(string sectionName, string path)
{
    ...
    return HttpConfigurationSystem.GetSection(sectionName, path);
}

WebConfigurationManager專為ASP.NET應用程序而設計。

WebConfigurationManager提供了其他方法來加載適用於Web應用程序的配置文件。

ConfigurationManager還提供了加載適用於“.exe”應用程序的配置文件的方法。

我建議你看一下WebConfigurationManager,看看它是否為你提供了一些你無法用ConfigurationManager做的事情而是使用它,否則使用ConfigurationManager會讓你的代碼在web和桌面aps之間無縫地使用變得更加容易。

雖然WebConfigurationManager位於System.Web程序集中,但它返回的ConnectionStringSettingsCollection位於System.Configuration中。

如果您收到錯誤

無法將帶有[]的索引應用於類型為“System.Configuration.ConnectionStringSettingsCollection”的表達式

在嘗試訪問數組索引時......

WebConfigurationManager.ConnectionStrings["Name"].ConnectionString

確保您具有對程序集System.Configuration的引用

不確定你對連接字符串的意思。

調用WebConfigurationManager.ConnectionStrings將返回System.Configuration.ConnectionStringSettingsCollection,這與您調用ConfigurationManager.ConnectionStrings時獲得的相同。

否則,正如XOR所說,它旨在處理多個分層web.configs,當您在應用程序中移動文件夾時,根據需要組合它們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM