// 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 System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Formatters.Json.Internal;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
namespace Microsoft.AspNetCore.Mvc
{
///
/// An action result which formats the given object as JSON.
///
public class JsonResult : ActionResult
{
///
/// Creates a new with the given .
///
/// The value to format as JSON.
public JsonResult(object value)
{
Value = value;
}
///
/// Creates a new with the given .
///
/// The value to format as JSON.
/// The to be used by
/// the formatter.
public JsonResult(object value, JsonSerializerSettings serializerSettings)
{
if (serializerSettings == null)
{
throw new ArgumentNullException(nameof(serializerSettings));
}
Value = value;
SerializerSettings = serializerSettings;
}
///
/// Gets or sets the representing the Content-Type header of the response.
///
public string ContentType { get; set; }
///
/// Gets or sets the .
///
public JsonSerializerSettings SerializerSettings { get; set; }
///
/// Gets or sets the HTTP status code.
///
public int? StatusCode { get; set; }
///
/// Gets or sets the value to be formatted.
///
public object Value { get; set; }
///
public override Task ExecuteResultAsync(ActionContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
var services = context.HttpContext.RequestServices;
var executor = services.GetRequiredService();
return executor.ExecuteAsync(context, this);
}
}
}