djordjedjukic
12/15/2017 - 10:11 AM

All values from Web.config in one place

All values from Web.config in one place

public static class AppSettingsHelper
    {
        public static string SomeStingValueFromWebConfig => Get<string>("SomeStringValue");
        public static int SomeIntValueFromWebConfig => Get<string>("SomeIntValue");        

        private static TValue Get<TValue>(string key)
        {
            try
            {
                var value = ConfigurationManager.AppSettings[key];

                if (string.IsNullOrWhiteSpace(value))
                {
                    return default(TValue);
                }

                return (TValue)Convert.ChangeType(value, typeof(TValue));
            }
            catch (Exception)
            {
                return default(TValue);
            }
        }
    }