• (实战篇)httphandler登场亮相


    项目中用到了很多httphandler,这里抓出来一个,不算壮丁,但同样足以说明问题,如下:

    (1)web.config中配置

    
    
     <httpHandlers>
          <add verb="*" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax"/>
          <add verb="*" path="FWMODALDIALOG.ASPX" type="CIT.OA.CommonUI.HttpHandler.FWHttpHandlerFactory, CIT.OA.CommonUI, Version=1.0.0.0, Culture=neutral"/>
          <add verb="*" path="FWAJAX.ASPX" type="CIT.OA.CommonUI.HttpHandler.FWHttpHandlerFactory, CIT.OA.CommonUI, Version=1.0.0.0, Culture=neutral"/>
     </httpHandlers>
    这里配置是何意,其实一目了然,就是说所有请求FWMODALDIALOG.ASPX页面的都交给CIT.OA.CommonUI.HttpHandler.FWHttpHandlerFactory整个类来处理,
    而所有请求FWAJAX.ASPX的也都交给CIT.OA.CommonUI.HttpHandler.FWHttpHandlerFactory处理
    
    
    
    
    

    (2)项目中何处用到了呢?以一个最简单的应用来说明
    来看一段脚本:
    var returnValue = PopSelect_pop_ModalDialog(DialogTitle, "SystemManage/ResourceOperate.aspx?type=add", "700", "300");
     if (returnValue != "refresh") return false;此脚本的意思就是打开一个模态窗口,有返回值则刷新父页面,来看PopSelect_pop_ModalDialog方法的定义function PopSelect_pop_ModalDialog(title, url, w, h) {
        var winreswidth = w;
        var winresheight = h;
        var aboutbox = "";
        var filename = "FWMODALDIALOG.ASPX?title=" + escape(title)
                + "&url=" + escape(url)
        aboutbox = showModalDialog(filename, window, "dialogWidth:" + winreswidth + "px; dialogHeight:" + winresheight + "px;unadorned:no;help:no;toolbar=no;menubar=no;location=no;status=no;scrollbars=1;resizable=1");
        //open(filename, "", "");
        return aboutbox;
    }


    此方法打开FWMODALDIALOG.ASPX页面,而由于我们在web.config里做了配置,请求将导向CIT.OA.CommonUI.HttpHandler.FWHttpHandlerFactory处理

    接下来看这个类的主要实现
     
    public class FWHttpHandlerFactory : IHttpHandlerFactory, IRequiresSessionState
        {
            #region IHttpHandlerFactory 成员
     
            /// <summary>
            /// 使工厂可以重用现有的处理程序实例。
            /// </summary>
            /// <param name="handler">要重用的 IHttpHandler 对象</param>
            public void ReleaseHandler(IHttpHandler handler) { }
     
            /// <summary>
            /// 返回实现 IHttpHandler 接口的类的实例
            /// </summary>
            /// <param name="context">HttpContext 类的实例,它提供对用于为 HTTP 请求提供服务的内部服务器对象(如 Request、Response、Session 和 Server)的引用。</param>
            /// <param name="requestType">客户端使用的 HTTP 数据传输方法(GET 或 POST)。 </param>
            /// <param name="url">所请求资源的 RawUrl。</param>
            /// <param name="pathTranslated">所请求资源的 PhysicalApplicationPath。</param>
            /// <returns>处理请求的新的 IHttpHandler 对象。</returns>
            public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
            {
                string filename = context.Request.FilePath;
                filename = filename.Substring(filename.LastIndexOf("/") + 1).ToUpper();
                //访问不同的 Page 用不同的 Class 处理
                switch (filename)
                {
                    case "FWAJAX.ASPX":
                        {
                            return new FWAJAXHander();
                        }
                    case "FWMODALDIALOG.ASPX":
                        {
                            return new FWModalDialog();
                        }
                    default:
                        {
                            return context.Handler;
                        }
                }
            }
     
            #endregion IHttpHandlerFactory 成员
        }


    此工厂类再次将请求导向到其它类处理,如下:
    
    
    public class FWModalDialog : IHttpHandler, IRequiresSessionState
        {
            #region 接口实现
     
            /// <summary>
            /// 通过实现 IHttpHandler 接口的自定义 HttpHandler 启用 HTTP Web 请求的处理。
            /// </summary>
            /// <param name="context"></param>
     
            #region public void ProcessRequest(HttpContext context)
     
            public void ProcessRequest(HttpContext context)
            {
                //初始化参数
                string strTitle = CommonFunc.ObjectToNullStr(context.Request["title"]);
                string strUrl = CommonFunc.ObjectToNullStr(context.Request["url"]);
                string strScrolling = CommonFunc.ObjectToNullStr(context.Request["scrolling"]);
                if (CommonFunc.IsNullString(strScrolling))
                {
                    strScrolling = "auto";
                }
                strTitle = CommonFunc.UrlDecode(strTitle);
                strUrl = CommonFunc.UrlDecode(strUrl);
                if (!strUrl.ToLower().StartsWith("http://"))
                {
                    strUrl = FWConfig.AppPath + strUrl;
                }
     
                //组成  ModalDialog 的页面
                HttpResponse hr = context.Response;
                hr.Expires = -1;
                //            hr.CacheControl="no-cache,must-revalidate";
                StringBuilder sbContext = new StringBuilder();
                sbContext.Append("<html>\r\n");
                sbContext.Append("<head>\r\n");
                sbContext.Append("<meta http-equiv=\"pragma\" content=\"no-cache\">");
                sbContext.Append("<meta http-equiv=\"Cache-Control\" content=\"no-cache,must-revalidate\">");
                sbContext.Append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
                sbContext.Append("<META HTTP-EQUIV=\"Expires\" CONTENT=\"0\"> ");
                sbContext.Append("<title>" + strTitle + "</title>\r\n");
                sbContext.Append("</head>\r\n");
                sbContext.Append("<body scroll=no topmargin=\"0\" leftmargin=\"0\" rightmargin=\"0\" bottommargin=\"0\">\r\n");
                sbContext.Append("<iframe id=\"FWDialogFrm\" name=\"FWDialogFrm\" src=\"" + strUrl + "\" frameborder=\"0\" scrolling=\""
                    + strScrolling + "\" width=\"100%\" height=\"100%\"></iframe>\r\n");
                sbContext.Append("</body>\r\n");
                sbContext.Append("</html>\r\n");
     
                hr.Write(sbContext.ToString());
            }
     
    
    至此就实现了通过一段脚本,打开模态窗口,而其如何打开的,则是通过httphandler进行处理后打开的,
    以上应用虽小,但以够说明问题,记下以作参考之用!

  • 相关阅读:
    1203 forms组件
    1128 聚合查询 orm字段及属性
    1127 模型层orm表操作
    1126 视图层与模板层
    1122 django中orm操作
    1121 Django操作
    1125 视图层
    搭建并行开发环境MPICH2
    Linpack之HPL测试 (HPL Benchmark)
    安装NetCDF及HDF5
  • 原文地址:https://www.cnblogs.com/jangwewe/p/2970616.html
Copyright © 2020-2023  润新知