public static void SendDataBoxScriptResponse(HttpContext context) { HttpRequest request = context.Request; HttpResponse response = context.Response; string clientKey = request.QueryString["ck"]; BoxResponses responses = SingletonHelper.GetApplicationProvider <BoxResponses>(); string script; if (!responses.ContainsKey(clientKey)) { script = "// no scripts"; } else { script = responses[clientKey].Script; } responses.Remove(clientKey); response.Clear(); response.Write(script); response.Flush(); response.SuppressContent = true; HttpContext.Current.ApplicationInstance.CompleteRequest(); //response.End(); }
public static void SendBoxResponse(HttpContext context, string templateName) { HttpResponse response = context.Response; string clientKey = context.Request.QueryString["ck"]; if (string.IsNullOrEmpty(clientKey)) { throw new JsonInvalidOperationException("No client key was specified"); } BoxResponses boxResponses = SingletonHelper.GetApplicationProvider <BoxResponses>(); if (IsScriptRequest(context.Request)) { response.Clear(); response.Write(boxResponses[clientKey].Script); boxResponses.Remove(clientKey); response.Flush(); response.SuppressContent = true; context.ApplicationInstance.CompleteRequest(); return; } string virtualBoxPath = GetVirtualBoxPath(context, templateName); try { if (string.IsNullOrEmpty(virtualBoxPath)) { throw new JsonInvalidOperationException("The box template file could not be found: [" + templateName + "]"); } // this is the dom id of the requesting "box" div element string requesterId = context.Request.QueryString["domid"]; // TODO: enumify magic strings BoxResponse boxResponse = GetBoxResponse(context, virtualBoxPath, null, true); boxResponses.Add(clientKey, boxResponse); response.Clear(); response.Write(boxResponse.Html); response.Flush(); response.SuppressContent = true; context.ApplicationInstance.CompleteRequest(); } catch (Exception ex) { SendBoxedError(context, ex); } return; }
/// <summary> /// This method will send the "ConglomerateScript" for the specified templateName. /// </summary> /// <remarks>A ConglomerateScript represents all the scripts required by the JsonControls /// in an ascx file. The BoxUserControl class prepares the resulting ascx control for /// Json/Box rendering.</remarks> /// <param name="context">The HttpContext significant to the current request.</param> /// <param name="templateName">The name of an ascx file contained in the /// BoxServer.BoxTemplateDirectory or the virutal path to an ascx file.</param> /// <returns></returns> //public static void SendBoxScriptResponse(HttpContext context, string templateName) //{ // string virtualBoxPath = GetVirtualBoxPath(context, templateName); // try // { // if (string.IsNullOrEmpty(virtualBoxPath)) // throw new InvalidOperationException("The box template could not be found: [" + templateName + "]"); // string requesterId = context.Request.QueryString["domid"]; // HttpResponse response = context.Response; // //string responseString = BoxServer.GetScriptString(virtualBoxPath, requesterId); // //GetDataBoxResponse(context, virtualBoxPath, null); // response.Clear(); // response.Write(responseString); // response.Flush(); // response.SuppressContent = true; // context.ApplicationInstance.CompleteRequest(); // } // catch (Exception ex) // { // SendBoxedError(context, ex); // } //} /// <summary> /// Applies a template to the Data property of the specified JsonResult object /// and sends the result to the client as an html section or "Box". /// </summary> /// <param name="context">The HttpContext</param> /// <param name="jsonResult">The result to be Boxed</param> public static void SendDataBoxResponse(HttpContext context, JsonResult jsonResult) { #region check JsonResult HttpResponse response = context.Response; // Check the status of the jsonResult // if status is error send default error box if (jsonResult.Status == JsonResultStatus.Error) { SendBoxedError(response, jsonResult); return; } if (jsonResult.Data == null) { SendBoxedError(response, jsonResult); return; } #endregion HttpRequest request = context.Request; string boxTemplateName = request.QueryString["dbx"];// TODO: create enums to represent all the magic strings used by the different script components string clientKey = request.QueryString["ck"]; if (string.IsNullOrEmpty(boxTemplateName)) { SendBoxedError(response, jsonResult); return; } object[] objectsToTemplate = new object[] { jsonResult.Data }; if (jsonResult.Data is Array) { objectsToTemplate = (object[])jsonResult.Data; } BoxResponses responses = SingletonHelper.GetApplicationProvider <BoxResponses>(); string allHtml = string.Empty; string allScript = string.Empty; try { foreach (object objectToTemplate in objectsToTemplate) { // getting the property variables for each object will allow objects of different types to // be templated in one pass. This incurs some processing overhead but the flexibility is // worth it. string[] propertyVariables = GetPropertyVariables(objectToTemplate.GetType()); string virtualBoxPath = GetVirtualDataBoxPath(context, objectToTemplate.GetType(), boxTemplateName); if (string.IsNullOrEmpty(virtualBoxPath)) { SendBoxedError(context, ExceptionHelper.CreateException <JsonInvalidOperationException>("Unable to find specified data box template ['{0}.{1}']", objectToTemplate.GetType().Name, boxTemplateName)); } BoxResponse dataBoxResponse = GetBoxResponse(context, virtualBoxPath, objectToTemplate, true); string html = dataBoxResponse.Html; string script = dataBoxResponse.Script; allHtml += GetVariableReplaceText(html, propertyVariables, objectToTemplate); allScript += GetVariableReplaceText(script, propertyVariables, objectToTemplate); } } catch (Exception ex) { SendBoxedError(context, ex); } responses.Add(clientKey, new BoxResponse(allHtml, allScript)); response.Clear(); response.Write(allHtml); response.Flush(); response.SuppressContent = true; HttpContext.Current.ApplicationInstance.CompleteRequest(); //response.End(); }