更好的方法通过jQuery,Ajax和ASP.Net返回数据块
我有一个ajax方法可以根据页面上设置的条件检索数据,这可以是很多记录。我改变了我的ajax调用来一次轮询25条记录,而不是返回一个大的数据集。更好的方法通过jQuery,Ajax和ASP.Net返回数据块
我这样做的方式是在我的WebMethod上,我根据条件运行sql,然后将行转换为JSON并保存到第一次轮询中的文件。后续的只是从文件加载回类,所以我可以选择下一个25.我需要在第一次调用时运行一次数据,我不能简单地选择25,因为函数需要加载所有记录来执行其他基于它的行动。
我现在的问题是,一个报告抛出错误返回一个非常大的一组数据:在使用JSON JavaScriptSerializer序列化或反序列化期间的错误。字符串的长度超过maxJsonLength属性中设置的值。
我已经有了最大设置,所以很清楚我需要考虑另一种方式来保存第一次运行的数据并将它发送回大块,请有任何想法吗?
编辑: 本质上,我正在寻找一种方法来保存类的文件,并在随后的请求检索它。
我已经改变了JSON正常序列化,像这样:
BinaryFormatter bf = new BinaryFormatter();
FileStream fileStream = new FileStream(System.Web.HttpContext.Current.Server.MapPath("path/data" + model.reportId + ".txt"), FileMode.Create);
bf.Serialize(fileStream, preview);
fileStream.Close();
和反序列化:
BinaryFormatter bf = new BinaryFormatter();
FileStream fileStream = new FileStream(System.Web.HttpContext.Current.Server.MapPath("path/data" + model.reportId + ".txt"), FileMode.Open);
fileStream.Seek(0, SeekOrigin.Begin);
DataPreview savedPreview = (DataPreview)bf.Deserialize(fileStream);
fileStream.Close();
在ASP.NET中,你可以访问所有强大cache
对象。
您可以轻松创建您的记录集,然后将其如下添加到缓存:
HttpContext.Current.Cache.Insert("UniqueidentifierForObject", TheRecordSetObject, Nothing, DateTime.Now.AddHours(2), Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, Nothing)
这将插入对象到缓存为2小时绝对过期。如果你四处看看,有很多关于缓存的好文档,所以我不打算在这里深入讨论。重要的是要注意缓存是如何访问的,因为在Web方法或服务中不会是正常的httpContext
,所以你需要使用扩展的引用。
然后在后续调用数据,你可以通过调用获取对象从缓存中:
Dataset theDataset = HttpContext.Current.Cache("UniqueidentifierForObject");
if(theDataset == null){
//CallYour load function because the cahce did not have your object
}else{
//Do you pull for the chunk of data.
}
这样,你只需做你的数据库调用一次,但可以从一组获得记录时你永远需要。
此外,重要的是要注意ASP.NET在web方法和服务中的序列化真的很好。 You don't normally have to serialize or deserialize data that is being passed from and too code behind in web methods and services。它通常是为你完成的,并试图再次这样做可能会导致各种奇怪的行为。
看看asp.net缓存 – will 2013-03-08 13:29:40
你有任何起始链接,因为有很多看看。你可以将它作为答案发布吗? – user1166905 2013-03-08 13:44:07