C# JsonResult在ASP.NET CORE 2.1中返回Json

C# JsonResult在ASP.NET CORE 2.1中返回Json,c#,json,asp.net-core-2.1,jsonresult,C#,Json,Asp.net Core 2.1,Jsonresult,在ASP.NET Core 2.0中工作的控制器: [Produces("application/json")] [Route("api/[controller]")] [ApiController] public class GraficResourcesApiController : ControllerBase { private readonly ApplicationDbContext _context; public GraficResourcesApiCo

在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);
    }
}
写入当前上下文中不存在Json。如果我们简单地删除Json

return rows;
然后写到无法显式地将类型List()转换为JsonResult

现在如何转换为Json?

中没有
Json(Object)
方法。然而,确实如此

因此,要么重构要从
controller

public class GraficResourcesApiController : 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);
}
要访问,或者您可以在操作中自己初始化一个新的

return new JsonResult(rows);
这基本上就是该方法在
Controller

public class GraficResourcesApiController : 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);
}
//
///创建序列化指定对象的对象
///到JSON。
/// 
///要序列化的对象。
///已创建用于序列化指定
///转换为响应的JSON格式。
[不行动]
公共虚拟JsonResult Json(对象数据)
{
返回新的JsonResult(数据);
}
/// 
///创建序列化指定对象的对象
///到JSON。
/// 
///要序列化的对象。
///要使用的
///格式化程序。
///已创建用于序列化指定
///作为响应的JSON格式。
///调用者应缓存的实例以避免
///在每次调用中重新创建缓存数据。
[不行动]
公共虚拟JsonResult Json(对象数据,JsonSerializerSettings serializerSettings)
{
如果(serializerSettings==null)
{
抛出新ArgumentNullException(nameof(serializerSettings));
}
返回新的JsonResult(数据、序列化设置);
}
中没有
Json(对象)
方法。然而,确实如此

因此,要么重构要从
controller

public class GraficResourcesApiController : 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);
}
要访问,或者您可以在操作中自己初始化一个新的

return new JsonResult(rows);
这基本上就是该方法在
Controller

public class GraficResourcesApiController : 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);
}
//
///创建序列化指定对象的对象
///到JSON。
/// 
///要序列化的对象。
///已创建用于序列化指定
///转换为响应的JSON格式。
[不行动]
公共虚拟JsonResult Json(对象数据)
{
返回新的JsonResult(数据);
}
/// 
///创建序列化指定对象的对象
///到JSON。
/// 
///要序列化的对象。
///要使用的
///格式化程序。
///已创建用于序列化指定
///作为响应的JSON格式。
///调用者应缓存的实例以避免
///在每次调用中重新创建缓存数据。
[不行动]
公共虚拟JsonResult Json(对象数据,JsonSerializerSettings serializerSettings)
{
如果(serializerSettings==null)
{
抛出新ArgumentNullException(nameof(serializerSettings));
}
返回新的JsonResult(数据、序列化设置);
}

因此,从2.0升级到2.1会破坏现有代码。有人知道他们为什么要改变这个吗?@WillC我不能告诉你他们为什么要改变它,但我可以告诉你,DNC背后的一个主要原因是他们可以破坏东西。快速迭代,很少保证向后兼容性。请注意,如果您正在创建API,文档中建议不要使用Controller,而是使用ControllerBase。Ref docs:
不要通过从控制器类派生来创建web API控制器。控制器源于ControllerBase并添加了对视图的支持,所以它用于处理web页面,而不是web API请求。这条规则有一个例外:如果您计划对视图和API使用同一个控制器,则从控制器派生它。
这很好,但返回JSON是一项基本的API活动,因此它不是他们推荐的API类的成员似乎很不寻常。因此,从2.0升级到2.1会破坏现有代码。有人知道他们为什么要改变这个吗?@WillC我不能告诉你他们为什么要改变它,但我可以告诉你,DNC背后的一个主要原因是他们可以破坏东西。快速迭代,很少保证向后兼容性。请注意,如果您正在创建API,文档中建议不要使用Controller,而是使用ControllerBase。Ref docs:
不要通过从控制器类派生来创建web API控制器。控制器源于ControllerBase并添加了对视图的支持,所以它用于处理web页面,而不是web API请求。这条规则有一个例外:如果您计划对视图和API使用同一个控制器,请从控制器派生它。
这很好,但返回JSON是一项基本的API活动,因此它不是他们推荐用于API的类的成员似乎很不寻常。