-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathExtensions.cs
53 lines (49 loc) · 1.62 KB
/
Extensions.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
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Text;
namespace Microsoft.AspNetCore
{
/// <summary>
/// Provides shared extensions for this project.
/// </summary>
static class Extensions
{
/// <summary>
/// Returns true if the HttpResponse is a 2xx response code.
/// </summary>
/// <param name="response">HttpResponse object.</param>
/// <returns></returns>
public static bool IsSuccessStatusCode(this HttpResponse response)
{
if (response.StatusCode >= 200 && response.StatusCode < 299)
return true;
return false;
}
/// <summary>
///
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
public static string SchemeAndHost(this Http.HttpRequest request, string appendQuery = null)
{
return string.Format("{0}://{1}{2}", request.Scheme, request.Host, request.Path);
}
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="query"></param>
/// <param name="key"></param>
/// <returns></returns>
public static T TryGet<T>(this IQueryCollection query, string key)
{
if (query.ContainsKey(key))
{
if (query.TryGetValue(key, out Microsoft.Extensions.Primitives.StringValues keyValue))
return (T)Convert.ChangeType(keyValue.ToString(), typeof(T));
}
throw new ArgumentNullException();
}
}
}