CoreConfig.cs 749 B

12345678910111213141516171819202122232425262728293031
  1. using Newtonsoft.Json;
  2. using System.Configuration;
  3. namespace Masuit.Tools.Config
  4. {
  5. public static class ConfigHelper
  6. {
  7. public static string GetConfigOrDefault(string key, string defaultValue = "")
  8. {
  9. return ConfigurationManager.AppSettings.Get(key) ?? defaultValue;
  10. }
  11. public static T Get<T>(string key)
  12. {
  13. var value = ConfigurationManager.AppSettings.Get(key);
  14. if (string.IsNullOrWhiteSpace(value))
  15. {
  16. return default;
  17. }
  18. try
  19. {
  20. return JsonConvert.DeserializeObject<T>(value);
  21. }
  22. catch
  23. {
  24. return default;
  25. }
  26. }
  27. }
  28. }