Question

ASP.NET

For each appSetting I use, I want to specify a value that will be returned if the specified key isn't found in the appSettings. I was about to create a class to manage this, but I'm thinking this functionality is probably already in the .NET Framework somewhere?

Is there a NameValueCollection/Hash/etc-type class in .NET that will let me specify a key and a fallback/default value -- and return either the key's value, or the specified value?

If there is, I could put the appSettings into an object of that type before calling into it (from various places).

Was it helpful?

Solution

I don't believe there's anything built into .NET which provides the functionality you're looking for.

You could create a class based on Dictionary<TKey, TValue> that provides an overload of TryGetValue with an additional argument for a default value, e.g.:

public class MyAppSettings<TKey, TValue> : Dictionary<TKey, TValue>
{
    public void TryGetValue(TKey key, out TValue value, TValue defaultValue)
    {
        if (!this.TryGetValue(key, out value))
        {
            value = defaultValue;
        }
    }
}

You could probably get away with strings instead of keeping in generic.

There's also DependencyObject from the Silverlight and WPF world if those are options.

Of course, the simplest way is something like this with a NameValueCollection:

string value = string.IsNullOrEmpty(appSettings[key]) 
    ? defaultValue 
    : appSettings[key];

key can be null on the string indexer. But I understand it's a pain to do that in multiple places.

OTHER TIPS

This is what I do.

WebConfigurationManager.AppSettings["MyValue"] ?? "SomeDefault")

For Boolean and other non-string types...

bool.Parse(WebConfigurationManager.AppSettings["MyBoolean"] ?? "false")

You can make a custom configuration section and provide default values using the DefaultValue attribute. Instructions for that are available here.

I think the machine.config under C:\%WIN%\Microsoft.NET will do this. Add keys to that file as your default values.

http://msdn.microsoft.com/en-us/library/ms228154.aspx

You can build logic around ConfigurationManager to get a typed way to retrieve your app setting value. Using here TypeDescriptor to convert value.

/// /// Utility methods for ConfigurationManager /// public static class ConfigurationManagerWrapper {

    /// <summary>
    /// Use this extension method to get a strongly typed app setting from the configuration file.
    /// Returns app setting in configuration file if key found and tries to convert the value to a specified type. In case this fails, the fallback value
    /// or if NOT specified - default value - of the app setting is returned
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="appsettingKey"></param>
    /// <param name="fallback"></param>
    /// <returns></returns>
    public static T GetAppsetting<T>(string appsettingKey, T fallback = default(T))
    {
        string val = ConfigurationManager.AppSettings[appsettingKey] ?? "";
        if (!string.IsNullOrEmpty(val))
        {
            try
            {
                Type typeDefault = typeof(T);
                var converter = TypeDescriptor.GetConverter(typeof(T));
                return converter.CanConvertFrom(typeof(string)) ? (T)converter.ConvertFrom(val) : fallback;
            }
            catch (Exception err)
            {
                Console.WriteLine(err); //Swallow exception
                return fallback;
            }
        }
        return fallback;
    }

}

}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top