CoreConfig.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #if NET461
  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. }
  12. }
  13. #else
  14. using Microsoft.Extensions.Configuration;
  15. using System;
  16. namespace Masuit.Tools.Config
  17. {
  18. /// <summary>
  19. /// .net core的配置导入
  20. /// </summary>
  21. public static class ConfigHelper
  22. {
  23. /// <summary>
  24. /// 配置对象
  25. /// </summary>
  26. public static IConfiguration Configuration { get; private set; } = new ConfigurationBuilder().SetBasePath(AppContext.BaseDirectory).AddJsonFile("appsettings.json", true, true).Build();
  27. public static string GetConfigOrDefault(string key, string defaultValue = "")
  28. {
  29. string config = Configuration[key];
  30. return config.IsNullOrEmpty() ? defaultValue : config;
  31. }
  32. /// <summary>
  33. /// 将配置添加到Masuit.Tools,若未调用,将自动加载默认的appsettings.json
  34. /// </summary>
  35. /// <param name="config"></param>
  36. public static void AddToMasuitTools(this IConfiguration config)
  37. {
  38. Configuration = config;
  39. }
  40. }
  41. }
  42. #endif