/// <summary> /// 将数据写入页面流 /// </summary> /// <param name="param"></param> private void WriteToBuffer(AjaxCallbackParam param) { try { info.CurrentPage.Response.Clear(); if (param != null) { info.CurrentPage.Response.Write(SerializationManager.SerializeJson(param)); } else { info.CurrentPage.Response.ContentType = "image/gif"; } info.CurrentPage.Response.Cache.SetNoStore(); info.CurrentPage.Response.Flush(); //info.CurrentPage.Response.End(); //完成请求 info.CurrentPage.Response.Close(); HttpContext.Current.ApplicationInstance.CompleteRequest(); } catch { //不处理异常 } }
private void WriteErrorMessage(string message) { AjaxCallbackParam param = new AjaxCallbackParam(message); param.Success = false; WriteToBuffer(param); }
/// <summary> /// 发送请求 /// </summary> public void SendRequest() { try { bool AjaxProcess = WebHelper.GetRequestParam <bool>(info.CurrentPage.Request, "X-Ajax-Process", false); if (!AjaxProcess) { WebHelper.RegisterPageCssFile(info.CurrentPage, info.CurrentPage.ClientScript.GetWebResourceUrl(typeof(AjaxPage), "MySoft.Web.Resources.pager.css")); //需要启用模板加载 if (info.EnableAjaxTemplate) { WebHelper.RegisterPageJsFile(info.CurrentPage, info.CurrentPage.ClientScript.GetWebResourceUrl(typeof(AjaxPage), "MySoft.Web.Resources.template.js")); } WebHelper.RegisterPageForAjax(info.CurrentPage, info.CurrentPage.Request.Path); } else { var callbackParams = GetCallbackParams(); //只有启用Ajax,才调用初始化方法 if (info.CurrentPage is IAjaxInitHandler) { (info.CurrentPage as IAjaxInitHandler).OnAjaxInit(callbackParams); } if (info.CurrentPage is IAjaxProcessHandler) { (info.CurrentPage as IAjaxProcessHandler).OnAjaxProcess(callbackParams); } bool AjaxRegister = WebHelper.GetRequestParam <bool>(info.CurrentPage.Request, "X-Ajax-Register", false); bool AjaxRequest = WebHelper.GetRequestParam <bool>(info.CurrentPage.Request, "X-Ajax-Request", false); bool AjaxLoad = WebHelper.GetRequestParam <bool>(info.CurrentPage.Request, "X-Ajax-Load", false); string AjaxKey = WebHelper.GetRequestParam <string>(info.CurrentPage.Request, "X-Ajax-Key", Guid.NewGuid().ToString()); if (AjaxRegister) { //将value写入Response流 WriteAjaxMethods(info.CurrentPage.GetType()); } else if (AjaxRequest) { string AjaxMethodName = WebHelper.GetRequestParam <string>(info.CurrentPage.Request, "X-Ajax-Method", null); if (CheckHeader(AjaxKey)) { AjaxCallbackParam value = InvokeMethod(info.CurrentPage, AjaxMethodName); //将value写入Response流 WriteToBuffer(value); } else { throw new AjaxException("Method \"" + AjaxMethodName + "\" Is Invoke Error!"); } } else if (AjaxLoad) { string AjaxControlPath = WebHelper.GetRequestParam <string>(info.CurrentPage.Request, "X-Ajax-Path", null); bool UseAjaxTemplate = WebHelper.GetRequestParam <bool>(info.CurrentPage.Request, "X-Ajax-Template", false); if (CheckHeader(AjaxKey)) { AjaxCallbackParam param = LoadAjaxControl(AjaxControlPath, UseAjaxTemplate); //将param写入Response流 WriteToBuffer(param); } else { throw new AjaxException("Control \"" + AjaxControlPath + "\" Is Load Error!"); } } } } catch (ThreadAbortException) { } catch (AjaxException ex) { WriteErrorMessage("AjaxError: " + ex.Message); } catch (BusinessException ex) { WriteErrorMessage(string.Format("[{0}]{1}", ex.Code, ex.Message)); } catch (Exception ex) { WriteErrorMessage(ex.Message); } }