zoukankan      html  css  js  c++  java
  • 关于AjaxPro的性能改进 【转】

    关于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));
    

    AjaxPro.2.dll,发现生成脚本中的src应该是这样的:
    src="/AjaxProTest/ajaxpro/prototype.ashx",前面多了一个AjaxProTest的目录,不然会报错

  • 相关阅读:
    java基础部分的一些有意思的东西。
    antdvue按需加载插件babelpluginimport报错
    阿超的烦恼 javaScript篇
    .NET E F(Entity Framework)框架 DataBase First 和 Code First 简单用法。
    JQuery获得input ID相同但是type不同的方法
    gridview的删除,修改,数据绑定处理
    jgGrid数据格式
    Cannot read configuration file due to insufficient permissions
    Invoke action which type of result is JsonResult on controller from view using Ajax or geJSon
    Entity model数据库连接
  • 原文地址:https://www.cnblogs.com/myssh/p/1452226.html
Copyright © 2011-2022 走看看