WebApi下做项目配置

举报
清雨小竹 发表于 2022/09/25 01:00:19 2022/09/25
【摘要】 using Newtonsoft.Json;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Web; namespace YFAPICommon.Libs{ public class Mer...

  
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Web;
  7. namespace YFAPICommon.Libs
  8. {
  9. public class MerchantConfig
  10. {
  11. public string Name { set; get; }
  12. public string MerchantID { set; get; }
  13. public string Pass { set; get; }
  14. }
  15. public class MerchantCache
  16. {
  17. private static Dictionary<string, MerchantConfig> merchantCache = null;
  18. private static string jsonfile = System.Web.Hosting.HostingEnvironment.MapPath(@"~/MerchantConfig.json");//JSON文件路径
  19. public static void initCache()
  20. {
  21. string jsonStr = System.IO.File.ReadAllText(jsonfile, Encoding.Default);
  22. merchantCache = JsonConvert.DeserializeObject<Dictionary<string,MerchantConfig>>(jsonStr);
  23. }
  24. public static MerchantConfig getMerchantConfig(string MerchantID)
  25. {
  26. if (merchantCache == null)
  27. initCache();
  28. MerchantConfig config = null;
  29. if (merchantCache.TryGetValue(MerchantID, out config))
  30. return config;
  31. else
  32. return null;
  33. }
  34. public static void clearCache()
  35. {
  36. merchantCache = null;
  37. }
  38. public static string addConfig(MerchantConfig input)
  39. {
  40. if (merchantCache == null)
  41. initCache();
  42. MerchantConfig config = null;
  43. if (merchantCache.TryGetValue(input.MerchantID, out config))
  44. {
  45. return "已存在不可添加";
  46. }
  47. else
  48. {
  49. merchantCache.Add(input.MerchantID,input);
  50. string jsonStr = JsonConvert.SerializeObject(merchantCache);
  51. System.IO.File.WriteAllText(jsonfile, jsonStr, Encoding.Default);
  52. return "添加成功";
  53. }
  54. }
  55. public static object getAllConfig()
  56. {
  57. if (merchantCache == null)
  58. initCache();
  59. return merchantCache;
  60. }
  61. }
  62. }

  
  1. {
  2. "333333": {
  3. "Name": "测试商户2",
  4. "MerchantID": "333333",
  5. "Pass": "xxxxxxxxxxxxxxx"
  6. },
  7. "1234567": {
  8. "Name": "测试商户",
  9. "MerchantID": "1234567",
  10. "Pass": "xxxxxxxxxxxxxxx"
  11. }
  12. }

 

文章来源: zzzili.blog.csdn.net,作者:清雨小竹,版权归原作者所有,如需转载,请联系作者。

原文链接:zzzili.blog.csdn.net/article/details/104959834

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。