JsonResult在ASP.NET CORE 2.1中返回Json

JsonResult在ASP.NET CORE 2.1中返回Json,第1张

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);}

资源



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5477672.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存