增加项目,分层
This commit is contained in:
1 parent
6e995a5bdb
commit
0a3e5af6a9
234 files changed
+26994
-1
No files matched your search
@@ -0,0 +1,102 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Store.Framework.Models
|
||||
{
|
||||
public class Cookies : Dictionary<string, string>
|
||||
{
|
||||
private readonly string oldCookie;
|
||||
public Cookies(string cookie)
|
||||
{
|
||||
oldCookie = cookie;
|
||||
SplitCookie(oldCookie);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Cookie字符串
|
||||
/// </summary>
|
||||
public string CookieString
|
||||
{
|
||||
get
|
||||
{
|
||||
return GetCookieString();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 拆分Cookie
|
||||
/// </summary>
|
||||
private void SplitCookie(string cookie)
|
||||
{
|
||||
this.Clear();
|
||||
Regex regCookie = new Regex("(?<key>.+?)=(?<value>.+?)(;|$)");
|
||||
if (regCookie.IsMatch(cookie))
|
||||
{
|
||||
MatchCollection matchs = regCookie.Matches(cookie);
|
||||
foreach (Match item in matchs)
|
||||
{
|
||||
string key = item.Groups["key"].Value.Trim();
|
||||
string value = item.Groups["value"].Value.Trim();
|
||||
if (!ContainsKey(key))
|
||||
{
|
||||
this.Add(key.Trim(), value.Trim());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定值
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="defvalue">默认值</param>
|
||||
/// <returns></returns>
|
||||
public string GetValue(string key, string defvalue)
|
||||
{
|
||||
if (ContainsKey(key))
|
||||
{
|
||||
return this[key];
|
||||
}
|
||||
return defvalue;
|
||||
}
|
||||
/// <summary>
|
||||
/// 添加或修改
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="value"></param>
|
||||
public void AddOrUpdate(string key, string value)
|
||||
{
|
||||
if (!this.ContainsKey(key))
|
||||
{
|
||||
Add(key, value);
|
||||
}
|
||||
else
|
||||
{
|
||||
this[key] = value;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取Cookies字符串
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public virtual string GetCookieString()
|
||||
{
|
||||
var list = this.Select(a => { return $"{a.Key}={a.Value}"; });
|
||||
return string.Join(";", list);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 隐式转换
|
||||
/// </summary>
|
||||
/// <param name="cookie"></param>
|
||||
public static implicit operator string (Cookies cookie)
|
||||
{
|
||||
return cookie.GetCookieString();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user