JsonResult 在 ASP.NET CORE 2.1 中返回 Json [英] JsonResult return Json in ASP.NET CORE 2.1

查看:31
本文介绍了JsonResult 在 ASP.NET CORE 2.1 中返回 Json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 ASP.NET Core 2.0 中运行的控制器:

Controller that worked in ASP.NET Core 2.0:

[Produces("application/json")]
[Route("api/[controller]")]
[ApiController]
public class GraficResourcesApiController : ControllerBase
{    
    private readonly ApplicationDbContext _context;

    public GraficResourcesApiController(ApplicationDbContext context)
    {
        _context = context;
    }

    [HttpGet]
    public JsonResult GetGrafic(int ResourceId)
    {
        var sheduling = new List<Sheduling>();


        var events = from e in _context.Grafic.Where(c=>c.ResourceId == ResourceId)
                     select new
                     {
                         id = e.Id,
                         title = e.Personals.Name,
                         start = e.DateStart,
                         end = e.DateStop,
                         color = e.Personals.Color,
                         personalId = e.PersonalId,
                         description = e.ClientName
                     };
        var rows = events.ToArray();

        return Json(rows);
    }
}

在 ASP.NET Core 2.1 中

in ASP.NET Core 2.1

return Json (rows);

写道 Json 在当前上下文中不存在.如果我们删除 Json 简单地离开

writes that Json does not exist in the current context. If we remove Json leaving simply

return rows;

然后写道不可能将类型 List() 显式转换为 JsonResult

then writes that it was not possible to explicitly convert the type List () to JsonResult

现在如何转换成Json?

How to convert to Json now?

推荐答案

ControllerBase 没有 Json(Object) 方法.但是 Controller 确实如此.

In asp.net-core-2.1 ControllerBase does not have a Json(Object) method. However Controller does.

所以要么重构当前控制器以从 Controller

So either refactor the current controller to be derived from Controller

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

有权访问 Controller.Json 方法 或者你可以初始化一个新的 JsonResult 自己在行动

to have access to the Controller.Json Method or you can initialize a new JsonResult yourself in the action

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

来源

这篇关于JsonResult 在 ASP.NET CORE 2.1 中返回 Json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆