首页 > 其他分享 >JsonResult

JsonResult

时间:2022-11-20 10:56:13浏览次数:48  
标签:JsonResult object value context using public

https://github.com/dotnet/aspnetcore/blob/39f0e0b8f40b4754418f81aef0de58a9204a1fe5/src/Mvc/Mvc.Core/src/JsonResult.cs#L13

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Text.Json;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.Extensions.DependencyInjection;

namespace Microsoft.AspNetCore.Mvc;

/// <summary>
/// An action result which formats the given object as JSON.
/// </summary>
public class JsonResult : ActionResult, IStatusCodeActionResult
{
    /// <summary>
    /// Creates a new <see cref="JsonResult"/> with the given <paramref name="value"/>.
    /// </summary>
    /// <param name="value">The value to format as JSON.</param>
    public JsonResult(object? value)
    {
        Value = value;
    }

    /// <summary>
    /// Creates a new <see cref="JsonResult"/> with the given <paramref name="value"/>.
    /// </summary>
    /// <param name="value">The value to format as JSON.</param>
    /// <param name="serializerSettings">
    /// The serializer settings to be used by the formatter.
    /// <para>
    /// When using <c>System.Text.Json</c>, this should be an instance of <see cref="JsonSerializerOptions" />.
    /// </para>
    /// <para>
    /// When using <c>Newtonsoft.Json</c>, this should be an instance of <c>JsonSerializerSettings</c>.
    /// </para>
    /// </param>
    public JsonResult(object? value, object? serializerSettings)
    {
        Value = value;
        SerializerSettings = serializerSettings;
    }

    /// <summary>
    /// Gets or sets the <see cref="Net.Http.Headers.MediaTypeHeaderValue"/> representing the Content-Type header of the response.
    /// </summary>
    public string? ContentType { get; set; }

    /// <summary>
    /// Gets or sets the serializer settings.
    /// <para>
    /// When using <c>System.Text.Json</c>, this should be an instance of <see cref="JsonSerializerOptions" />
    /// </para>
    /// <para>
    /// When using <c>Newtonsoft.Json</c>, this should be an instance of <c>JsonSerializerSettings</c>.
    /// </para>
    /// </summary>
    public object? SerializerSettings { get; set; }

    /// <summary>
    /// Gets or sets the HTTP status code.
    /// </summary>
    public int? StatusCode { get; set; }

    /// <summary>
    /// Gets or sets the value to be formatted.
    /// </summary>
    public object? Value { get; set; }

    /// <inheritdoc />
    public override Task ExecuteResultAsync(ActionContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException(nameof(context));
        }

        var services = context.HttpContext.RequestServices;
        var executor = services.GetRequiredService<IActionResultExecutor<JsonResult>>();
        return executor.ExecuteAsync(context, this);
    }
}
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#nullable enable

using Microsoft.AspNetCore.Http;

namespace Microsoft.AspNetCore.Mvc.Infrastructure;

/// <summary>
/// Defines an interface for a service which can execute a particular kind of <see cref="IActionResult"/> by
/// manipulating the <see cref="HttpResponse"/>.
/// </summary>
/// <typeparam name="TResult">The type of <see cref="IActionResult"/>.</typeparam>
/// <remarks>
/// Implementations of <see cref="IActionResultExecutor{TResult}"/> are typically called by the
/// <see cref="IActionResult.ExecuteResultAsync(ActionContext)"/> method of the corresponding action result type.
/// Implementations should be registered as singleton services.
/// </remarks>
public interface IActionResultExecutor<in TResult> where TResult : notnull, IActionResult
{
    /// <summary>
    /// Asynchronously executes the action result, by modifying the <see cref="HttpResponse"/>.
    /// </summary>
    /// <param name="context">The <see cref="ActionContext"/> associated with the current request."/></param>
    /// <param name="result">The action result to execute.</param>
    /// <returns>A <see cref="Task"/> which represents the asynchronous operation.</returns>
    Task ExecuteAsync(ActionContext context, TResult result);
}

 

标签:JsonResult,object,value,context,using,public
From: https://www.cnblogs.com/qqhfeng/p/16908015.html

相关文章