您的位置:首页 > Web前端 > JQuery

结合jquery,json很好地利用ashx文件开发高性能ajax

2008-08-19 14:53 399 查看
原文:http://www.cnblogs.com/losingrose/articles/1247399.html

众所周知,webservice的性能要比常规httprequest处理速度要慢的多,而且传输数据量也要大很多。
项目开始的时候,我们为了节省时间,采用了微软的atlas+webservice,但是后来发现其性能确实不是很优秀。随者项目的稳定,我在网上寻找更优化的方法,看到jquery在网上的口碑比较好,于是就试用了下,发现其兼容性和性能确实很好,而且服务器端不使用webservice,而采用比aspx处理还要快的ashx处理程序,大大提高了处理速度。json转化类是网上找的Newtonsoft.Json.dll,不过我又找代码补充了几个函数。现在做前台模块优化工作,又找到了一个好用的东西 Javascript Template(JST),可以编写模板,绑定json,可以很方便的制作和维护前台ajax板块。
关于类的设计,我采用了一个基类Service_Base,其中包含一些转换和输出的函数:
#region JsonConvert

public string Json(DataTable table)

{

return DatatableToJson.CreateJsonParameters(table);

}

public string Json(params DataTable[] table)

{

return DatatableToJson.JSON_DataTables(table);

}

#endregion

#region TypeConvert

public int intPrase(string value)

{

int x = 0;

int.TryParse(value, out x);

return x;

}

public bool boolParse(string value)

{

if (value.ToLower() == "true")

return true;

else

return false;

}

public DateTime datetimePrase(string value)

{

DateTime datetime = DateTime.Now;

DateTime.TryParse(value, out datetime);

return datetime;

}

#endregion

public void WriteStream(object str)

{

HttpContext.Current.Response.ContentType = "text/plain";

HttpContext.Current.Response.Clear();

HttpContext.Current.Response.Write(str);

HttpContext.Current.Response.Flush();

HttpContext.Current.Response.Close();

}
然后写方法实体类继承此基类:public class ServiceUser : global::Service_Base,在其中创建逻辑层函数
public int UserNameTest(string UserName)

{

BusUser bl = new BusUser();

if (bl.CheckUserName(UserName))

return 1;

else

return 0;

}
页面接口采用ashx,继承业务逻辑层:

using System;

using System.Web;

using System.Web.SessionState;
public class Security : ServiceUser, IHttpHandler, IRequiresSessionState
并实现接口:
public void ProcessRequest(HttpContext context)

{

string method = context.Request.Form["m"];

if (method == "UserNameTest")

{

string UserName = context.Request.Form["UserName"];

WriteStream(UserNameTest(UserName));

}
else

{

WriteStream("请不要尝试非法的访问!");

return;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: