WebApi下做项目配置
【摘要】
using Newtonsoft.Json;using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Web; namespace YFAPICommon.Libs{ public class Mer...
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
namespace YFAPICommon.Libs
{
public class MerchantConfig
{
public string Name { set; get; }
public string MerchantID { set; get; }
public string Pass { set; get; }
}
public class MerchantCache
{
private static Dictionary<string, MerchantConfig> merchantCache = null;
private static string jsonfile = System.Web.Hosting.HostingEnvironment.MapPath(@"~/MerchantConfig.json");//JSON文件路径
public static void initCache()
{
string jsonStr = System.IO.File.ReadAllText(jsonfile, Encoding.Default);
merchantCache = JsonConvert.DeserializeObject<Dictionary<string,MerchantConfig>>(jsonStr);
}
public static MerchantConfig getMerchantConfig(string MerchantID)
{
if (merchantCache == null)
initCache();
MerchantConfig config = null;
if (merchantCache.TryGetValue(MerchantID, out config))
return config;
else
return null;
}
public static void clearCache()
{
merchantCache = null;
}
public static string addConfig(MerchantConfig input)
{
if (merchantCache == null)
initCache();
MerchantConfig config = null;
if (merchantCache.TryGetValue(input.MerchantID, out config))
{
return "已存在不可添加";
}
else
{
merchantCache.Add(input.MerchantID,input);
string jsonStr = JsonConvert.SerializeObject(merchantCache);
System.IO.File.WriteAllText(jsonfile, jsonStr, Encoding.Default);
return "添加成功";
}
}
public static object getAllConfig()
{
if (merchantCache == null)
initCache();
return merchantCache;
}
}
}
{
"333333": {
"Name": "测试商户2",
"MerchantID": "333333",
"Pass": "xxxxxxxxxxxxxxx"
},
"1234567": {
"Name": "测试商户",
"MerchantID": "1234567",
"Pass": "xxxxxxxxxxxxxxx"
}
}
文章来源: zzzili.blog.csdn.net,作者:清雨小竹,版权归原作者所有,如需转载,请联系作者。
原文链接:zzzili.blog.csdn.net/article/details/104959834
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
作者其他文章
评论(0)