Skip to content

Instantly share code, notes, and snippets.

@djordjedjukic
Last active December 15, 2017 10:12
Show Gist options
  • Save djordjedjukic/427e9c6a6c52e01ac21cf187c69a4b9c to your computer and use it in GitHub Desktop.
Save djordjedjukic/427e9c6a6c52e01ac21cf187c69a4b9c to your computer and use it in GitHub Desktop.
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);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment