devxlogo

GetWebConfigCustomKeyValue – Return and cache a custom key’s value from Web.Config

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

See also  Does It Make Sense to Splurge on a Laptop?
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist