我自己整了个小东西,用aps.net mvc做到页面,数据使用ado.net Entityframwork,我用的应该算Code first,我先写了几个实体类,用实体类生成的数据,没有连接sql server,直接用的LocalDB,数据的提取代码如下: [OutputCache(Duration = 60, VaryByParam = "*")]
public ActionResult guohuwenda(int id = 1, int index = 1)
{
//传入进来的id 是类型id,index是页面的页码
int s , x;
List<articles> l = GetArticels(out s, out x, index, id);
ViewBag.Previous = s;
ViewBag.Next = x;
ViewBag.CurrentIndex = index;
ViewBag.categroy = id;
return View(l);
}
/// <summary>
///
/// </summary>
/// <param name="s">上一页是否有效</param>
/// <param name="x">下一页是否有效</param>
/// <param name="p">当前的页码,当前第几页</param>
/// <param name="rid">类型id,数据类型</param>
/// <returns></returns>
public List<articles> GetArticels(out int s,out int x,int p = 1,int rid=0)
{
int pageSize = SysConfig.WenDaListPagesize, pageIndex = p-1;
List<articles> list = new List<articles>();
if (rid == 0 || rid == 1)
{
var result = from i in DB.articless
orderby i.CreateTime descending
select i;
int total = result.Count();
s = p == 1 ? 0 : 1;
if ((p * pageSize) >= total) x = 0; else x = 1;
list = result.OrderByDescending(r => r.CreateTime).Skip(pageSize * pageIndex).Take(pageSize).ToList(); ;//.Skip(pageSize * pageIndex).Take(pageSize);
}
else {
var result = from i in DB.articless
where i.classID== rid
orderby i.CreateTime descending
select i;
int total = result.Count();
s = p == 1 ? 0 : 1;
if ((p * pageSize) >= total) x = 0; else x = 1;
list = result.OrderByDescending(r => r.CreateTime).Skip(pageSize * pageIndex).Take(pageSize).ToList(); ;//.Skip(pageSize * pageIndex).Take(pageSize);
}
//list = result.ToList();
return list;
}
DB 是在BaseController中的一个属性:
private ApplicationDbContext _db;
/// <summary>
/// 共用的数据库对象
/// </summary>
public ApplicationDbContext DB
{
get {
if (_db == null)
{
if (SysCache.GetCache(SysConfig.Sys_dbBaseCache) != null)
{
_db = (ApplicationDbContext)SysCache.GetCache(SysConfig.Sys_dbBaseCache);
}
else
{
_db = new ApplicationDbContext();
SysCache.SetCache(SysConfig.Sys_dbBaseCache, _db);
}
}
return _db;
}
}
求大师们帮我分析分析,这个到底咋个的
网页访问这个列表页面,每次加载时间至少是10秒以上,这个太不科学了。没有找到原因哦!
这个是发布后的效果,很慢啊
http://www.kdtianxia.com/qicheguohu/guohuwenda/1/1
另外也说说我的服务器,感觉不应该是服务器的问题,因为其他的页面都没有这么慢,阿里云的单核服务器,1M带宽,2G内存。
public ActionResult guohuwenda(int id = 1, int index = 1)
{
//传入进来的id 是类型id,index是页面的页码
int s , x;
List<articles> l = GetArticels(out s, out x, index, id);
ViewBag.Previous = s;
ViewBag.Next = x;
ViewBag.CurrentIndex = index;
ViewBag.categroy = id;
return View(l);
}
/// <summary>
///
/// </summary>
/// <param name="s">上一页是否有效</param>
/// <param name="x">下一页是否有效</param>
/// <param name="p">当前的页码,当前第几页</param>
/// <param name="rid">类型id,数据类型</param>
/// <returns></returns>
public List<articles> GetArticels(out int s,out int x,int p = 1,int rid=0)
{
int pageSize = SysConfig.WenDaListPagesize, pageIndex = p-1;
List<articles> list = new List<articles>();
if (rid == 0 || rid == 1)
{
var result = from i in DB.articless
orderby i.CreateTime descending
select i;
int total = result.Count();
s = p == 1 ? 0 : 1;
if ((p * pageSize) >= total) x = 0; else x = 1;
list = result.OrderByDescending(r => r.CreateTime).Skip(pageSize * pageIndex).Take(pageSize).ToList(); ;//.Skip(pageSize * pageIndex).Take(pageSize);
}
else {
var result = from i in DB.articless
where i.classID== rid
orderby i.CreateTime descending
select i;
int total = result.Count();
s = p == 1 ? 0 : 1;
if ((p * pageSize) >= total) x = 0; else x = 1;
list = result.OrderByDescending(r => r.CreateTime).Skip(pageSize * pageIndex).Take(pageSize).ToList(); ;//.Skip(pageSize * pageIndex).Take(pageSize);
}
//list = result.ToList();
return list;
}
DB 是在BaseController中的一个属性:
private ApplicationDbContext _db;
/// <summary>
/// 共用的数据库对象
/// </summary>
public ApplicationDbContext DB
{
get {
if (_db == null)
{
if (SysCache.GetCache(SysConfig.Sys_dbBaseCache) != null)
{
_db = (ApplicationDbContext)SysCache.GetCache(SysConfig.Sys_dbBaseCache);
}
else
{
_db = new ApplicationDbContext();
SysCache.SetCache(SysConfig.Sys_dbBaseCache, _db);
}
}
return _db;
}
}
求大师们帮我分析分析,这个到底咋个的
网页访问这个列表页面,每次加载时间至少是10秒以上,这个太不科学了。没有找到原因哦!
这个是发布后的效果,很慢啊
http://www.kdtianxia.com/qicheguohu/guohuwenda/1/1
另外也说说我的服务器,感觉不应该是服务器的问题,因为其他的页面都没有这么慢,阿里云的单核服务器,1M带宽,2G内存。
解决方案 »
- 原来发布后使用正常的网站,重新发布会出错吗?
- 高分在线等待:多表字段追加和汇总的sql语句
- 求救,用AJAX判断用户名是否存在
- 一个判断邮箱的正则问题
- 关于VS2005中Datalist里放了RadioBottonList的绑定问题
- <%if (Model != null) 中的Model是什么意思
- ============高分跪求!请高手过来看看。。
- [高分求源码]小妹想求一个关于asp.netHTML在线编辑器使用方面的例子,都一天了还是不知道怎么搞.哪位高手做过这种例子的,请借小妹看一下.
- 从数据库下载附件后无法关闭IE窗口问题
- 很奇怪的问题
- MVC+ORM 视图提交 部分更新数据的问题
- socket客户端不停监控 和接受服务端发来的消息
if (SysCache.GetCache(SysConfig.Sys_dbBaseCache) != null)
{
_db = (ApplicationDbContext)SysCache.GetCache(SysConfig.Sys_dbBaseCache);
}
上面代码判断和赋值连续调用两次相同方法,这其实可以只调用1次