zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    Linux内存管理 -- /proc/{pid}/smaps讲解
    link hub(other)
    牛客项目平台管家 | xie_note 学习笔记整理📚 项目来源:https://github.com/Making-It/note ,已获得授权转载
    【Linux】C++后台开发面试
    C++ 后台开发面试时一般考察什么?
    Linux C/C++ 学习路线(已拿腾讯、百度 offer)2
    C++路线图
    【转】C++后台开发校招面试常见问题
    【转】Linux C/C++ 学习路线(已拿腾讯、百度 offer)
    学习经验总结|C++后台开发/云计算方向,offer收割机的学习路线
  • 原文地址:https://www.cnblogs.com/RuiLei/p/1711771.html
Copyright © 2011-2022 走看看