JsonResult在ASP.NET CORE 2.1中返回Json

bla*_*cat 22 c# json jsonresult asp.net-core-2.1

在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);
    }
}
Run Code Online (Sandbox Code Playgroud)

在ASP.NET Core 2.1中

return Json (rows);
Run Code Online (Sandbox Code Playgroud)

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

return rows;
Run Code Online (Sandbox Code Playgroud)

然后写道,无法将类型List()显式转换为JsonResult

现在怎么转换成Json?

Nko*_*osi 40

在 ControllerBase中没有Json(Object)方法.不过Controller.

因此要么重构要派生的当前控制器 Controller

public class GraficResourcesApiController : Controller {
    //...
}
Run Code Online (Sandbox Code Playgroud)

有权访问Controller.Json方法,或者您可以JsonResult在操作中初始化自己

return new JsonResult(rows);
Run Code Online (Sandbox Code Playgroud)

这基本上是该方法在内部的作用 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);
}
Run Code Online (Sandbox Code Playgroud)

资源

  • 请注意,如果您要创建 API,文档中建议不要使用 Controller,而应使用 ControllerBase。参考文档:“不要通过从 Controller 类派生来创建 Web API 控制器。Controller 派生自 ControllerBase 并添加了对视图的支持,因此它用于处理网页,而不是 Web API 请求。此规则有一个例外:如果您计划对视图和 API 使用相同的控制器,请从 Controller 派生它。 (3认同)
  • 这很好,但返回 JSON 是一项基本的 API 活动,因此它不是他们为 API 推荐的类的成员,这似乎很不寻常。 (2认同)