在.Net Framework的system.Web.Configuration命名空間下有新的類別:WebconfigurationmManager可用於存取Web.config設定檔的所有內容 Web.config檔中的每個區段都繼承自configurationSection 類別,且每個區段對應的類別名稱就是該區段名加上section。例如:ConnectionStringsSection 類別就對應到web.config 設定檔的<ConnectionStrings> 區段 若要取得下列區段內容
c#語法如下
//將cnSection 明確的轉型為 ConnectionStringsSection型別 ConnectionStringsSection cnSection = (ConnectionStringsSection)WebConfigurationManager.GetWebApplicationSection("connectionStrings"); string NWind = cnSection.ConnectionStrings["Northwind"].ConnectionString; //string NWind = WebConfigurationManager.ConnectionStrings["Northwind"].ConnectionString; Response.Write(NWind);vb 語法如下
Dim cnect As ConnectionStringsSection = WebConfigurationManager.GetWebApplicationSection("connectionStrings") Dim NWind As String = cnect.ConnectionStrings("Northwind").ConnectionString 'Dim NWind As String = WebConfigurationManager.ConnectionStrings("Northwind").ConnectionString Response.Write(NWind)參考線上說明