-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollection.cs
More file actions
156 lines (144 loc) · 5.85 KB
/
Collection.cs
File metadata and controls
156 lines (144 loc) · 5.85 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
#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>
/// 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);
}
}