随笔 - 785  文章 - 16 评论 - 39 阅读 - 166万
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

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);
}
复制代码

 

posted on   qqhfeng16  阅读(160)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
历史上的今天:
2017-11-20 串口通信,帧与帧之间的时间间隔问题?9600波特率,帧将各在20ms以上
点击右上角即可分享
微信分享提示