// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.AspNetCore.Mvc.Core;
using Microsoft.AspNetCore.Mvc.Infrastructure;
namespace Microsoft.AspNetCore.Mvc
{
///
/// A type that wraps either an instance or an .
///
/// The type of the result.
public sealed class ActionResult : IConvertToActionResult
{
///
/// Initializes a new instance of using the specified .
///
/// The value.
public ActionResult(TValue value)
{
if (typeof(IActionResult).IsAssignableFrom(typeof(TValue)))
{
var error = Resources.FormatInvalidTypeTForActionResultOfT(typeof(TValue), "ActionResult");
throw new ArgumentException(error);
}
Value = value;
}
///
/// Initializes a new instance of using the specified .
///
/// The .
public ActionResult(ActionResult result)
{
if (typeof(IActionResult).IsAssignableFrom(typeof(TValue)))
{
var error = Resources.FormatInvalidTypeTForActionResultOfT(typeof(TValue), "ActionResult");
throw new ArgumentException(error);
}
Result = result ?? throw new ArgumentNullException(nameof(result));
}
///
/// Gets the .
///
public ActionResult Result { get; }
///
/// Gets the value.
///
public TValue Value { get; }
public static implicit operator ActionResult(TValue value)
{
return new ActionResult(value);
}
public static implicit operator ActionResult(ActionResult result)
{
return new ActionResult(result);
}
IActionResult IConvertToActionResult.Convert()
{
return Result ?? new ObjectResult(Value)
{
DeclaredType = typeof(TValue),
};
}
}
}