AjaxPro In WebApp

本文属于小结,主要有二个目的

1.网上一些人说AjaxPro在WebApplication中无法应用的问题

2.简化AjaxPro注册加载方式(基于性能方面的考虑)

 

1.AjaxPro在WebApplication中配置

  • Web.Config

             <httpHandlers>
        <!--Framework 3.5 Default HttpHandlers—>
          …
          <!--AjaxPro HttpHandlers—>
       <add verb="*" path="*.ashx" type="AjaxPro.AjaxHandlerFactory,AjaxPro.2"/>

      </httpHandlers>

  • Ref AjaxPro.2.dll
  • Page CodeBehind

      Page_Load Event中添加注册

      AjaxPro.Utility.RegisterTypeForAjax ( typeof ( _Default ) );

      声明方法

               [AjaxPro.AjaxMethod]
       public string getValue ( int a , int b ) {
           return Convert.ToString ( a+b );
       }

  • Page

      脚本应用

      <input id="Button1" type="button" value="button" onclick="getValue()" />

       <script type="text/javascript" language="javascript">
         function getValue() {
             NameSpace._Default.getValue(1, 2, getGroups_callback);
         }
         function getGroups_callback(response) {
             var dt = response.value;
             alert(dt);
         }
    </script>

 

2.简化AjaxPro注册、优化加载

   引文:

       使用AjaxPro的时候,首先要将包含Ajax方法的类注册的页面上,这样做的效果是很好的将面向对象的概念与js结合起来,

       但是很可惜,注册这个方法却会让你付出昂贵的代价。注册这个类的时候,它会向页面上注册几段脚本。

      首先:代价损失是整个注册过程,至少会耗掉你200ms以上,

      其次:这个方法作为.ashx文件在服务器端通过httpHandlers处理为一些js文件,

          有3个文件是AjaxPro的核心部分,它们分别是:prototype.ashx,core.ashx 以及converter.ashx。

          3个核心文件大约会占用29.9k以上,加上httpHandlers处理的时间,页面性能比较差。

  改进:

            合并三个核心js文件为一个文件,并进行压缩。

            采用RegisterStartupScript来替换AjaxPro源码中的RegisterScriptBlock的加载方式(避免阻塞页面加载

            简化AjaxPro注册使用,采用Attribute方式

  代码:

  •         Page

       <form id="form1" runat="server">
          <div>
              <input id="Button1" type="button" value="button" onclick="getValue()" />
          </div>
       </form>

       <script type="text/javascript" language="javascript">
          function getValue() {  

            NameSpace._Default.getValue(1, 2, getGroups_callback); 

          }
          function getGroups_callback(response) {
            var dt = response.value;
            alert(dt);

          }
       </script>


  •     Page CodeBehind

    namespace NameSpace{

       [RegAjaxPro ( typeof ( _Default) , "脚本路径" )]
       public partial class _Default: System.Web.UI.Page {

          protected void Page_Load (object sender , EventArgs e) {
          }

          [AjaxMethod]
          public string getValue (int a , int b) {
            return Convert.ToString ( a + b );
          }
        }
      }

     

  •     Attribute Class Code 

    [AttributeUsage ( AttributeTargets.Class , AllowMultiple = true , Inherited = true )]
    public class RegAjaxProAttribute : Attribute {
       public RegAjaxProAttribute (Type type , String jsPath) {
         var assemblyName = String.Concat ( type.FullName , "," ,
                                              type.Assembly.FullName.Substring ( 0 ,
                                              type.Assembly.FullName.IndexOf ( "," ) ) );

         var page = System.Web.HttpContext.Current.Handler as System.Web.UI.Page;

         var js = @" <script type='text/javascript' language='javascript' src='{0}'></script>";

         page.ClientScript.RegisterStartupScript ( page.GetType () , "ajaxPro2min" ,
              String.Format ( js , String.Concat ( jsPath , @"ajaxpro2.min.js" ) ).ToString () );


         page.ClientScript.RegisterStartupScript ( page.GetType () , "ajaxProAssm" ,
          String.Format ( js , String.Concat ( "/ajaxpro/" , assemblyName , ".ashx" ) ).ToString () );

       }
    }

结束!

代码下载地址:https://files.cnblogs.com/RuiLei/AjaxProWebApp.rar

posted @ 2010-04-14 13:37  RicoRui  阅读(632)  评论(3编辑  收藏  举报