Leggere configurazioni ASP.NET del file web.config con asp 3.0

In questo articolo vedremo come rendere semplice lacondivisione di settaggi tra un' applicazione in ASP.NETed un' applicazione in ASP classico e la migrazione daun' applicazione in ASP ad una in ASP.NET. Impareremoinfatti come permettere ad una applicazione in ASP classicodi leggere impostazioni e stringhe di connessione relative aduna più recente applicazione scritta in ASP.NET. Perfar ciò basterà che venga utilizzato dall'applicazione .NET, come sorgente contenente impostazioni estringhe di connessione, il file  web.config

Il codice

Innanzitutto bisogna sapere che, nel file web.config, i dativengono immagazzinati in formato XML. Come vedremodirettamente dal codice, le due funzioni sono piuttostosimili, se non fosse che muta la posizione dei settaggi nellastruttura dell' XML, così come mutano, dal puntodi vista della sintassi, le stringhe di connessione e leimpostazioni dell' applicazione.

Da notare che le funzioni, così strutturate, sono casesensitive, quindi c'è da prestare attenzione allemaiuscole ed alle minuscole dei settaggi richiamati e dellastringa di connessione richiesta: se non coincidono conquelle contenute nel file web.config l' applicazioneandrà in errore.

Ecco il codice di readsettings.asp:

<%@ Language="VBScript" %><%Option ExplicitFunction GetAppSetting(strAppSettingKey)  Dim xmlWebConfig  Dim nodeAppSettings  Dim nodeChildNode  Dim strAppSettingValue  Set xmlWebConfig = Server.CreateObject("Msxml2.DOMDocument.6.0")  xmlWebConfig.async = False  xmlWebConfig.Load(Server.MapPath("/Web.config"))  If xmlWebConfig.parseError.errorCode = 0 Then    Set nodeAppSettings = xmlWebConfig.selectSingleNode("//configuration/appSettings")    For Each nodeChildNode In nodeAppSettings.childNodesIf nodeChildNode.getAttribute("key") = strAppSettingKey Then  strAppSettingValue = nodeChildNode.getAttribute("value")  Exit ForEnd If    Next    Set nodeAppSettings = Nothing  End If  Set xmlWebConfig = Nothing  GetAppSetting = strAppSettingValueEnd FunctionFunction GetConnectionString(strConnStringName)  Dim xmlWebConfig  Dim nodeConnStrings  Dim nodeChildNode  Dim strConnStringValue  Set xmlWebConfig = Server.CreateObject(&quot;Msxml2.DOMDocument.6.0")  xmlWebConfig.async = False  xmlWebConfig.Load(Server.MapPath("/Web.config"))  If xmlWebConfig.parseError.errorCode = 0 Then    Set nodeConnStrings = xmlWebConfig.selectSingleNode("//configuration/connectionStrings")    For Each nodeChildNode In nodeConnStrings.childNodesIf nodeChildNode.getAttribute("name") = strConnStringName Then  strConnStringValue = nodeChildNode.getAttribute("connectionString")  Exit ForEnd If    Next    Set nodeConnStrings = Nothing  End If  Set xmlWebConfig = Nothing  GetConnectionString = strConnStringValueEnd Function%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head>  <title>Lettura dei settaggi di un' applicazione in ASP.NET usando l' ASP classico</title></head><body><p>I seguenti valori sono tratti dal file <code>web.config</code>posto nella directory principale del sito.</p><p>Messaggio di benvenuto: <%= GetAppSetting("MessaggioBenvenuto") %></p><p>Semplice stringa di connessione: <%= GetConnectionString("StringaConnessione") %></p><p>Messaggio di ringraziamento: <%= GetAppSetting("MessaggioSaluto") %></p></body></html>

Sebbene nel 90% dei casi verrà utilizzato il fileweb.config già presente nel webserver, riportiamo diseguito un possibile esempio del file, compatibile con l'applicazione che intendiamo creare:

<?xml version="1.0"?><configuration>  <appSettings>    <add key="MessaggioBenvenuto" value="Benvenuto nel nostro sito." />    <add key="MessaggioSaluto" value="Grazie della vista... torna a trovarci!." />  </appSettings>  <connectionStrings>    <add name="StringaConnessione"providerName="System.Data.OleDbClient"connectionString="Provider=Microsoft.Jet.OLEDB.4.0;  Data Source=C:InetpubwwwrootApp_Data    est.mdb;"    />  </connectionStrings>  <system.web>    <compilation debug="false" />  </system.web></configuration>