Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: VB7
Expertise: Intermediate
Dec 16, 2002



Building the Right Environment to Support AI, Machine Learning and Deep Learning

GetWebConfigCustomKeyValue - Return and cache a custom key's value from Web.Config

' Return the value of the specified custom key, stored in the Web.Config file.
' If the value has been already requested before, the function returns its 
' cached value.
' The second optional parameter is the path of the Web.Config file where this 
' custom key is stored, and it is necessary to add a dependency to that file,
'  so that the cached value is discarded if the file is edited.
' Example: Dim connString = GetWebConfigCustomKeyValue("ConnString")

Function GetWebConfigCustomKeyValue(ByVal key As String, _
    Optional ByVal webConfigUrl As String = "/Web.Config") As String
    ' if this is not the first time this value is needed,
    '  we can find it in the cache
    Dim value As String = CType(HttpContext.Current.Cache(key), String)

    ' if the retrieved string is empty, the value is not present into the cache,
    '  thus it is retrieved it from Web.Config, and then cached
    If value Is Nothing OrElse value = "" Then
        value = ConfigurationSettings.AppSettings(key)
        HttpContext.Current.Cache.Insert(key, value, _
            New Caching.CacheDependency(webConfigUrl))
    End If

    Return value
End Function
Marco Bellinaso
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date