【问题标题】:Controller.Json function slow for IQueryableIQueryable 的 Controller.Json 功能慢
【发布时间】:2016-03-30 23:08:43
【问题描述】:

我有一个返回 json 的 mvc 控制器操作,它需要大约 2 秒,我希望这会更快,不到 1 秒。我正在分析控制器操作,发现返回 json 的行很慢,直到它执行不到 100 毫秒。 是因为我使用 IQueryable 接口使用实际的 sql 到 linq 查询到达该行,并且只执行 DB 查询并且它执行类似“toList()”的操作。我想知道那里实际发生了什么? 还是 Controller.Json 函数通常很慢,我可以使用更好的东西?

 public ActionResult GetItems()
{
    IQueryable<Item> Items = default(IQueryable<Item>);
      Items = myComponent.getItems(); //returns IQueryable
     var result = Items.OrderByDescending(m => m.category).ThenBy(m => m.order);
      return Json(result, JsonRequestBehavior.AllowGet);
  }

【问题讨论】:

  • 如果getItems() 返回IQueryable,它可能还没有命中数据库。该方法在做什么?
  • 我更新了我的问题。它是一个 MVC 控制器,返回一个 json 数据。
  • 显示getItems()的代码,如果速度慢,问题就出在那儿。
  • 你的意思是它会在稍后被执行,即使在我只返回 Iquerable 时快速跳过确切的行。

标签: json asp.net-mvc


【解决方案1】:

有两个地方可以调音:

  1. 可自行查询
  2. Controller.Json的序列化方法

对于 IQuerable 查询,您可以尝试 sql profiler 或尝试在返回之前调用 ToList() 以查看需要多长时间并可能对其进行优化 (如果不了解您的查询,我无法帮助您)。

但对于第二部分,您可以尝试使用 Json.Net 之类的库对结果进行序列化

从source 代码中可以看出,当你调用Json() 时,Mvc 使用JavaScriptSerializer 进行json 序列化。 (你也可以通过查看源代码来了解实际发生的情况)

从 json.net 网站的对比图中,JavaScriptSerializer 的表现相当糟糕。

所以你可以尝试使用 Json.Net 实现你自己的 JsonActionResult,下面是一些示例代码:

来源:Using JSON.NET to return ActionResult

在您的控制器(或基本控制器)中

protected override JsonResult Json(object data, string contentType, System.Text.Encoding contentEncoding, JsonRequestBehavior behavior)
{
    return new JsonNetResult
    {
        Data = data,
        ContentType = contentType,
        ContentEncoding = contentEncoding,
        JsonRequestBehavior = behavior
    };
}

以及 JsonNetResult 的定义:

public class JsonNetResult : JsonResult
{
    public JsonNetResult()
    {
        Settings = new JsonSerializerSettings
        {
            ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
        };
    }

    public JsonSerializerSettings Settings { get; private set; }

    public override void ExecuteResult(ControllerContext context)
    {
        if (context == null)
            throw new ArgumentNullException("context");
        if (this.JsonRequestBehavior == JsonRequestBehavior.DenyGet && string.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
            throw new InvalidOperationException("JSON GET is not allowed");

        HttpResponseBase response = context.HttpContext.Response;
        response.ContentType = string.IsNullOrEmpty(this.ContentType) ? "application/json" : this.ContentType;

        if (this.ContentEncoding != null)
            response.ContentEncoding = this.ContentEncoding;
        if (this.Data == null)
            return;

        var scriptSerializer = JsonSerializer.Create(this.Settings);

        using (var sw = new StringWriter())
        {
            scriptSerializer.Serialize(sw, this.Data);
            response.Write(sw.ToString());
        }
    }
}

【讨论】:

    猜你喜欢
    • 2021-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-19
    • 1970-01-01
    • 2020-11-12
    • 2017-08-01
    • 1970-01-01
    相关资源
    最近更新 更多