Last active
December 15, 2017 10:12
-
-
Save djordjedjukic/427e9c6a6c52e01ac21cf187c69a4b9c to your computer and use it in GitHub Desktop.
All values from Web.config in one place
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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