您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

JsonResult在ASP.NET CORE 2.1中返回Json

JsonResult在ASP.NET CORE 2.1中返回Json

asp.net-core-2.1 ControllerBase中没有Json(Object)方法。但是Controller确实如此。

因此,要么将当前控制器重构为 Controller

public class GraficResourcesApiController : Controller {
    //...
}

可以访问该Controller.Json方法,或者您可以JsonResult在操作中初始化一个新的自己

return new JsonResult(rows);

这基本上是该方法在内部执行的操作 Controller

/// <summary>
/// Creates a <see cref="JsonResult"/> object that serializes the specified <paramref name="data"/> object
/// to JSON.
/// </summary>
/// <param name="data">The object to serialize.</param>
/// <returns>The created <see cref="JsonResult"/> that serializes the specified <paramref name="data"/>
/// to JSON format for the response.</returns>
[NonAction]
public virtual JsonResult Json(object data)
{
    return new JsonResult(data);
}

/// <summary>
/// Creates a <see cref="JsonResult"/> object that serializes the specified <paramref name="data"/> object
/// to JSON.
/// </summary>
/// <param name="data">The object to serialize.</param>
/// <param name="serializerSettings">The <see cref="JsonSerializerSettings"/> to be used by
/// the formatter.</param>
/// <returns>The created <see cref="JsonResult"/> that serializes the specified <paramref name="data"/>
/// as JSON format for the response.</returns>
/// <remarks>Callers should cache an instance of <see cref="JsonSerializerSettings"/> to avoid
/// recreating cached data with each call.</remarks>
[NonAction]
public virtual JsonResult Json(object data, JsonSerializerSettings serializerSettings)
{
    if (serializerSettings == null)
    {
        throw new ArgumentNullException(nameof(serializerSettings));
    }

    return new JsonResult(data, serializerSettings);
}

资源

dotnet 2022/1/1 18:12:31 有821人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶