-
Notifications
You must be signed in to change notification settings - Fork 717
Expand file tree
/
Copy pathHttpContentExtensions.cs
More file actions
43 lines (38 loc) · 1.44 KB
/
HttpContentExtensions.cs
File metadata and controls
43 lines (38 loc) · 1.44 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
// Copyright (c) .NET Foundation and contributors. All rights reserved.
namespace Asp.Versioning;
#if !NETFRAMEWORK
using Microsoft.AspNetCore.Mvc;
#endif
using System.Net.Http;
#if NETFRAMEWORK
using System.Net.Http.Formatting;
#else
using System.Net.Http.Json;
#endif
internal static class HttpContentExtensions
{
#if NETFRAMEWORK
private static readonly JsonMediaTypeFormatter ProblemDetailsMediaTypeFormatter = new()
{
SupportedMediaTypes = { new( ProblemDetailsDefaults.MediaType.Json ) },
};
private static readonly IEnumerable<MediaTypeFormatter> MediaTypeFormatters = new[] { ProblemDetailsMediaTypeFormatter };
#endif
public static Task<ProblemDetails> ReadAsProblemDetailsAsync(
this HttpContent content,
CancellationToken cancellationToken = default ) =>
#if NETFRAMEWORK
content.ReadAsAsync<ProblemDetails>( MediaTypeFormatters, cancellationToken );
#else
content.ReadFromJsonAsync<ProblemDetails>( cancellationToken );
#endif
#pragma warning disable IDE0060 // Remove unused parameter
#pragma warning disable IDE0079 // Remove unnecessary suppression
public static Task<T> ReadAsExampleAsync<T>(
this HttpContent content,
T example,
CancellationToken cancellationToken = default ) =>
content.ReadAsAsync<T>( cancellationToken );
#pragma warning restore IDE0060 // Remove unused parameter
#pragma warning restore IDE0079 // Remove unnecessary suppression
}