-
Notifications
You must be signed in to change notification settings - Fork 0
/
Collection.cs
155 lines (143 loc) · 5.92 KB
/
Collection.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#region Related components
using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections.Specialized;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Extensions.Primitives;
using Microsoft.Extensions.Logging;
using net.vieapps.Components.Caching;
#endregion
namespace net.vieapps.Components.Utility
{
/// <summary>
/// Static servicing methods for working with ASP.NET Core collections
/// </summary>
public static partial class AspNetCoreCollectionService
{
/// <summary>
/// Adds an item into ASP.NET Core Session
/// </summary>
/// <param name="session"></param>
/// <param name="key"></param>
/// <param name="value"></param>
public static void Add(this ISession session, string key, object value)
{
if (!string.IsNullOrWhiteSpace(key))
try
{
session.Set(key.ToLower(), Helper.Serialize(value));
}
catch (Exception ex)
{
Logger.Log<ISession>(LogLevel.Debug, LogLevel.Warning, $"Cannot add object into session: {ex.Message}", ex);
}
}
/// <summary>
/// Gets an item from ASP.NET Core Session
/// </summary>
/// <param name="session"></param>
/// <param name="key"></param>
/// <returns></returns>
public static object Get(this ISession session, string key)
=> !string.IsNullOrWhiteSpace(key) && session.TryGetValue(key.ToLower(), out var value)
? Helper.Deserialize(value)
: null;
/// <summary>
/// Gets an item from ASP.NET Core Session
/// </summary>
/// <param name="session"></param>
/// <param name="key"></param>
/// <returns></returns>
public static T Get<T>(this ISession session, string key)
=> !string.IsNullOrWhiteSpace(key) && session.TryGetValue(key.ToLower(), out var value)
? Helper.Deserialize<T>(value)
: default;
/// <summary>
/// Checks to see the key is existed in ASP.NET Core Session or not
/// </summary>
/// <param name="session"></param>
/// <param name="key"></param>
/// <returns></returns>
public static bool ContainsKey(this ISession session, string key)
=> !string.IsNullOrWhiteSpace(key) && session.Keys.FirstOrDefault(k => k.IsEquals(key.ToLower())) != null;
/// <summary>
/// Sets an object into this context items
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="context"></param>
/// <param name="name"></param>
/// <returns></returns>
public static void SetItem<T>(this HttpContext context, string name, T value)
=> context.Items[name] = value;
/// <summary>
/// Gets an object from this context items
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="context"></param>
/// <param name="name"></param>
/// <returns></returns>
public static T GetItem<T>(this HttpContext context, string name, T @default = default)
=> context.Items.TryGetValue(name, out var value) && value is T val
? val
: @default;
/// <summary>
/// Converts this dictionary of string values to dictionary of string
/// </summary>
/// <param name="dictionary"></param>
/// <param name="onCompleted">The action to run before completed</param>
/// <returns></returns>
public static Dictionary<string, string> ToDictionary(this IDictionary<string, StringValues> dictionary, Action<Dictionary<string, string>> onCompleted = null)
{
var dict = dictionary.ToDictionary(kvp => kvp.Key.ToLower(), kvp => kvp.Value.Where(@string => @string != null).Select(@string => @string.AsciiDecode()).Join(","), StringComparer.OrdinalIgnoreCase);
onCompleted?.Invoke(dict);
return dict;
}
/// <summary>
/// Converts this dictionary of string values to collection of name and value
/// </summary>
/// <param name="dictionary"></param>
/// <param name="onCompleted">The action to run before completed</param>
/// <returns></returns>
public static NameValueCollection ToNameValueCollection(this IDictionary<string, StringValues> dictionary, Action<NameValueCollection> onCompleted = null)
{
var nvCollection = new NameValueCollection();
dictionary.ToDictionary(dict => dict.ForEach(kvp => nvCollection[kvp.Key] = kvp.Value));
onCompleted?.Invoke(nvCollection);
return nvCollection;
}
/// <summary>
/// Converts this header to a dictionary of string
/// </summary>
/// <param name="header"></param>
/// <param name="onCompleted">The action to run before completed</param>
/// <returns></returns>
public static Dictionary<string, string> ToDictionary(this IHeaderDictionary header, Action<Dictionary<string, string>> onCompleted = null)
=> (header as IDictionary<string, StringValues>).ToDictionary(onCompleted);
/// <summary>
/// Converts this header to collection of name and value
/// </summary>
/// <param name="header"></param>
/// <param name="onCompleted">The action to run before completed</param>
/// <returns></returns>
public static NameValueCollection ToNameValueCollection(this IHeaderDictionary header, Action<NameValueCollection> onCompleted = null)
=> (header as IDictionary<string, StringValues>).ToNameValueCollection(onCompleted);
/// <summary>
/// Converts this query string to a dictionary of string
/// </summary>
/// <param name="queryString"></param>
/// <param name="onCompleted">The action to run before completed</param>
/// <returns></returns>
public static Dictionary<string, string> ToDictionary(this QueryString queryString, Action<Dictionary<string, string>> onCompleted = null)
=> QueryHelpers.ParseQuery(queryString.ToUriComponent()).ToDictionary(onCompleted);
/// <summary>
/// Converts this query string to collection of name and value
/// </summary>
/// <param name="queryString"></param>
/// <param name="onCompleted">The action to run before completed</param>
/// <returns></returns>
public static NameValueCollection ToNameValueCollection(this QueryString queryString, Action<NameValueCollection> onCompleted = null)
=> QueryHelpers.ParseQuery(queryString.ToUriComponent()).ToNameValueCollection(onCompleted);
}
}