Truly
写精彩代码 品暇逸人生

关于AjaxPro的性能改进
作者:Truly

偶然发现AjaxPro的页面注册是相当花费时间的方法

初步对AjaxPro方法进行分析,它的页面注册方法

AjaxPro.Utility.RegisterTypeForAjax(typeof(命名空间.类名));


是往页面上注册几段脚本块,

<script type="text/javascript" src="/ajaxpro/prototype.ashx"></script>
<script type="text/javascript" src="/ajaxpro/core.ashx"></script>
<script type="text/javascript" src="/ajaxpro/converter.ashx"></script>
<script type="text/javascript" src="/ajaxpro/Space_For_Static,App_Web_gz3bdi8j.ashx"></script>

前3段是AjaxPro的内核函数,而最后一段则是我们页面上的Ajax方法封装集合。

事实上我们的网站是发布的站点,而这些Ajax方法基本上是写在业务层代码中的,所以发布之后并不需要经常修改。

注册2段Ajax方法集大约要花费300ms左右,这个花费是十分惊人的,所以我们不应该这样使用AjaxPro

AjaxPro的原理是当对“/ajaxpro/Space_For_Static,App_Web_gz3bdi8j.ashx”这样的路径请求时,生成指定类中Ajax方法集合,所以我们只需直接给页面增加这个脚本块地址即可。

要给页面增加这个块,有几种方法,使用<%= str %>,或者使用ClientScript.RegisterClientScriptBlock(.net 2.0)/RegisterClientScriptBlock(.net 1.x),但是根据我的测试,使用后者虽然比较简单而且安全,但是性能很差,所以推荐使用前者,同时我推荐另一种方法:

Literal l = new Literal();
l.Text = "<script type=\"text/javascript\" src=\"/ajaxpro/prototype.ashx\"></script>"
+  "<script type=\"text/javascript\" src=\"/ajaxpro/core.ashx\"></script>"
+  "<script type=\"text/javascript\" src=\"/ajaxpro/converter.ashx\"></script>"
+  "<script type=\"text/javascript\" src=\"/ajaxpro/ABC.Notes,ABC.ashx\"></script>";
Header.Controls.Add(l);


注意:以上代码适用于.Net 2.0

使用这个方法的好处是你不再需要修改.aspx,而且可以通过页面基类一次性给所以页面进行注册,性能非常好,大约15-30ms左右,这样性能至少提高了10倍!!

 

比如我在PageBase方法中定义了RegAjax方法:
public void RegAjax(Type type)
{
// 用来代替 AjaxPro.Utility.RegisterTypeForAjax(Type type);方法
string assemblyName = type.FullName + "," + type.Assembly.FullName.Substring(0, type.Assembly.FullName.IndexOf(","));
if (type.Assembly.FullName.StartsWith("App_Code."))
assemblyName = type.FullName + ",App_Code";
Literal l = new Literal();
l.Text = "\n<script type=\"text/javascript\" src=\"/ajaxpro/prototype.ashx\"></script>\n"
+ "<script type=\"text/javascript\" src=\"/ajaxpro/core.ashx\"></script>\n"
+ "<script type=\"text/javascript\" src=\"/ajaxpro/converter.ashx\"></script>\n"
+ "<script type=\"text/javascript\" src=\"/ajaxpro/" + assemblyName + ".ashx\"></script>\n";
Header.Controls.Add(l);
}

然后我们在页面上使用
        RegAjax(typeof(Mythos.QianXun.BusinessRules.Notes));

来代替原始的
        AjaxPro.Utility.RegisterTypeForAjax(typeof(Mythos.QianXun.BusinessRules.Notes));
posted on 2006-11-20 18:16  Truly  阅读(1412)  评论(2编辑  收藏  举报