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 @   RicoRui  阅读(638)  评论(3编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
历史上的今天:
2008-04-14 AjaxToolKit-- 页面间跳转引发Exception
2007-04-14 仙剑四--IT的同仁有,是仙剑迷的请浏览
点击右上角即可分享
微信分享提示