-
Notifications
You must be signed in to change notification settings - Fork 9.8k
/
ActionResultOfT.cs
101 lines (87 loc) · 3.26 KB
/
ActionResultOfT.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
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc.Core;
using Microsoft.AspNetCore.Mvc.Infrastructure;
namespace Microsoft.AspNetCore.Mvc;
/// <summary>
/// A type that wraps either an <typeparamref name="TValue"/> instance or an <see cref="ActionResult"/>.
/// </summary>
/// <typeparam name="TValue">The type of the result.</typeparam>
public sealed class ActionResult<TValue> : IConvertToActionResult
{
private const int DefaultStatusCode = StatusCodes.Status200OK;
/// <summary>
/// Initializes a new instance of <see cref="ActionResult{TValue}"/> using the specified <paramref name="value"/>.
/// </summary>
/// <param name="value">The value.</param>
public ActionResult(TValue value)
{
if (typeof(IActionResult).IsAssignableFrom(typeof(TValue)) ||
typeof(IResult).IsAssignableFrom(typeof(TValue)))
{
var error = Resources.FormatInvalidTypeTForActionResultOfT(typeof(TValue), "ActionResult<T>");
throw new ArgumentException(error);
}
Value = value;
}
/// <summary>
/// Initializes a new instance of <see cref="ActionResult{TValue}"/> using the specified <see cref="ActionResult"/>.
/// </summary>
/// <param name="result">The <see cref="ActionResult"/>.</param>
public ActionResult(ActionResult result)
{
if (typeof(IActionResult).IsAssignableFrom(typeof(TValue)) ||
typeof(IResult).IsAssignableFrom(typeof(TValue)))
{
var error = Resources.FormatInvalidTypeTForActionResultOfT(typeof(TValue), "ActionResult<T>");
throw new ArgumentException(error);
}
Result = result ?? throw new ArgumentNullException(nameof(result));
}
/// <summary>
/// Gets the <see cref="ActionResult"/>.
/// </summary>
public ActionResult? Result { get; }
/// <summary>
/// Gets the value.
/// </summary>
public TValue? Value { get; }
/// <summary>
/// Implicitly converts the specified <paramref name="value"/> to an <see cref="ActionResult{TValue}"/>.
/// </summary>
/// <param name="value">The value to convert.</param>
public static implicit operator ActionResult<TValue>(TValue value)
{
return new ActionResult<TValue>(value);
}
/// <summary>
/// Implicitly converts the specified <paramref name="result"/> to an <see cref="ActionResult{TValue}"/>.
/// </summary>
/// <param name="result">The <see cref="ActionResult"/>.</param>
public static implicit operator ActionResult<TValue>(ActionResult result)
{
return new ActionResult<TValue>(result);
}
IActionResult IConvertToActionResult.Convert()
{
if (Result != null)
{
return Result;
}
int statusCode;
if (Value is ProblemDetails problemDetails && problemDetails.Status != null)
{
statusCode = problemDetails.Status.Value;
}
else
{
statusCode = DefaultStatusCode;
}
return new ObjectResult(Value)
{
DeclaredType = typeof(TValue),
StatusCode = statusCode
};
}
}