-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollection.cs
More file actions
206 lines (190 loc) · 7.47 KB
/
Collection.cs
File metadata and controls
206 lines (190 loc) · 7.47 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#region Related components
using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections.Specialized;
using Microsoft.AspNetCore.Diagnostics;
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 T SetItem<T>(this HttpContext context, string name, T value)
{
context.Items[name] = value;
return 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 bool TryGetItem<T>(this HttpContext context, string name, out T value)
{
value = default;
if (context.Items.TryGetValue(name, out var val) && val is T tvalue)
{
value = tvalue;
return true;
}
return false;
}
/// <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.TryGetItem<T>(name, out var value) ? value : @default;
/// <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 StatusCodeContext context, string name, T @default = default)
=> context.HttpContext.GetItem(name, @default);
/// <summary>
/// Removes an object from this context items
/// </summary>
/// <param name="context"></param>
/// <param name="name"></param>
/// <returns></returns>
public static bool RemoveItem(this HttpContext context, string name)
=> !string.IsNullOrWhiteSpace(name) && context.Items.Remove(name);
/// <summary>
/// Removes more objects from this context items
/// </summary>
/// <param name="context"></param>
/// <param name="names"></param>
/// <returns></returns>
public static bool RemoveItems(this HttpContext context, IEnumerable<string> names)
{
var result = names?.Select(name => context.RemoveItem(name));
return result != null && !result.Any(value => value == false);
}
/// <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);
}
}