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 valueEnd Function

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


Recent Articles: