【发布时间】:2014-02-18 02:39:58
【问题描述】:
我有一个 WebAPI 网络服务。我正在返回一个 Json 字符串。控制器返回 ActionResult,但我也尝试将其设为 JsonRequest。这不影响结果。
这里是相关代码(记住函数原型只是返回ActionResult:
Debug.WriteLine(string.Format("{0} - get campaign data: {1}", DateTime.Now.ToString("hh:mm:ss.fff"), 1));
// Here is the data we're going to need
var currentUser = context.Users.Where(n => n.UserID == userId).Single();
var locations = context.Locations.Where(n => n.CampaignID == campaign.CampaignID && n.Inactive == false);
var questions = context.Questions.Where(n => n.CampaignID == campaign.CampaignID && n.Inactive == false).Include(n => n.QuestionType).ToList().OrderBy(q=> q.SortOrder);
var dispositions = context.Dispositions.Where(n => n.CampaignID == campaign.CampaignID).ToList().OrderBy(d=> d.SortOrder);
var answers = context.Answers.Where(a => a.Inactive == false).Join(locations, answer => answer.LocationID, loc => loc.LocationID, (ans, loc) => ans).ToList();
var contacts = context.Contacts.Where(c => c.Inactive == false);
Debug.WriteLine(string.Format("{0} - get campaign data: {1}", DateTime.Now.ToString("hh:mm:ss.fff"), 2));
var json = new
{
success = true,
data = new
{
User = currentUser.ToModel(),
Campaign = campaign.ToModel(),
Location = locations.ToList().ToModelsSpecial(answers, contacts),
Question = questions.OrderBy(q => q.SortOrder).ToModelsSpecial(),
Disposition = dispositions.ToModels(),
},
message = (string)null
};
Debug.WriteLine(string.Format("{0} - get campaign data: {1}", DateTime.Now.ToString("hh:mm:ss.fff"), 3));
var response = Json(json, JsonRequestBehavior.AllowGet);
Debug.WriteLine(string.Format("{0} - get campaign data: {1}", DateTime.Now.ToString("hh:mm:ss.fff"), 4));
return response;
您可以看到我有 Debug.WriteLine 用于调试,它输出需要多长时间以便我可以跟踪它。
所以问题是包括数据库查询在内的所有处理只需要大约 2 秒。在那段时间里,它一直贯穿整个事情。
但我又过了 25 秒才在客户端上得到结果。我正在使用像 Fiddler 这样的程序来测试它。这一切都在我的本地机器上,所以互联网不是问题。
返回的 Json 字符串大小为 800k。由于这都是本地的,我不希望这需要 25 秒。
我在 Windows 8 上的 IIS8 中运行该站点。
谁能告诉我为什么 ActionResult 需要这么长时间才能返回给客户端?
【问题讨论】:
-
谁在使用这个调用 - jquery ajax 调用或其他一些客户端?
-
我想知道数据是如何呈现的。如果该表附加到 DOM,则向 html 表重复添加数千行很容易使浏览器慢下来。
标签: asp.net-mvc json actionresult