-
-
Notifications
You must be signed in to change notification settings - Fork 722
/
HttpResponseExtensions.cs
46 lines (41 loc) · 1.36 KB
/
HttpResponseExtensions.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
using System.Text.Json;
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Http;
using static System.Globalization.CultureInfo;
using static System.String;
namespace HotChocolate.AspNetCore;
internal static class HttpResponseExtensions
{
private const string _contentDepositionHeader = "Content-Disposition";
private const string _contentDepositionValue = "attachment; filename=\"{0}\"";
private static readonly JsonSerializerOptions _serializerOptions = new()
{
#if NETCOREAPP3_1
IgnoreNullValues = true,
#else
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
#endif
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};
internal static Task WriteAsJsonAsync<TValue>(
this HttpResponse response,
TValue value,
CancellationToken cancellationToken = default)
{
response.ContentType = ContentType.Json;
response.StatusCode = 200;
return JsonSerializer.SerializeAsync(
response.Body,
value,
_serializerOptions,
cancellationToken);
}
public static IHeaderDictionary SetContentDisposition(
this IHeaderDictionary headers,
string fileName)
{
headers[_contentDepositionHeader] =
Format(InvariantCulture, _contentDepositionValue, fileName);
return headers;
}
}