Example #1
0
        public static void ScriptPreRender(System.Web.UI.ScriptManager objScriptManager)
        {
            if (!objScriptManager.IsInAsyncPostBack) //I added this on 7/1/14, it seems like a safe idea
            {
                #region Debugging Clock
                DateTime dtStart = DateTime.Now;
                #endregion

                string strPage         = GetPageKey(objScriptManager);
                string strFileRelative = "Scripts/Compiled/" + strPage + ".js";
                string strFile         = objScriptManager.Page.MapPath(strFileRelative);

                if (objScriptManager.Scripts.Count > 0)
                {
                    if (!File.Exists(strFile))
                    {
                        //General.Debug.Write("Starting Script Compilation");
                        CompileClientScript(objScriptManager);
                        //General.Debug.Write("Finished Script Compilation");
                    }
                    else
                    {
                        if (!HashMatch(objScriptManager))
                        {
                            //General.Debug.Write("Starting Script Re-Compilation");
                            CompileClientScript(objScriptManager);
                            //General.Debug.Write("Finished Script Re-Compilation");
                        }
                    }
                    objScriptManager.Scripts.Clear();
                    if (objScriptManager.CompositeScript.Scripts.Count > 0)
                    {
                        objScriptManager.CompositeScript.Scripts.Add(new System.Web.UI.ScriptReference(strFileRelative));
                    }
                    else
                    {
                        objScriptManager.Scripts.Add(new System.Web.UI.ScriptReference(strFileRelative));
                    }
                }
                else
                {
                    if (File.Exists(strFile))
                    {
                        //General.Debug.Write("Deleting Old Script");
                        File.Delete(strFile);
                    }
                }

                #region Debugging Clock
                DateTime dtEnd = DateTime.Now;
                TimeSpan time  = dtEnd - dtStart;
                //General.Debug.JQueryDebugWrite("ScriptPreRender (" + time.TotalMilliseconds + "ms)");
                #endregion
            }
        }
Example #2
0
 public static void ScriptUnload(System.Web.UI.ScriptManager objScriptManager)
 {
     /*
      * if (General.Environment.Current.ExecuteJavascriptQueue != String.Empty)
      * {
      *  objScriptManager.Page.RegisterStartupScript("DLLLibraryScripts", "<script type=\"text/javascript\">" + General.Environment.Current.ExecuteJavascriptQueue + "</script>");
      *  //System.Web.HttpContext.Current.Response.Write("<script type=\"text/javascript\">" + General.Environment.Current.ExecuteJavascriptQueue + "</script>");
      *  General.Environment.Current.ExecuteJavascriptQueue = String.Empty;
      * }
      */
 }
Example #3
0
        /*
         * public static FileInfo CompileClientScript(string strWebPage, string strTargetFile)
         * {
         *  ArrayList objScripts = new ArrayList();
         *  string source = File.ReadAllText(strWebPage);
         *  string directory;
         *  string strBody = "";
         *  FileInfo objPageInfo = new FileInfo(strWebPage);
         *  directory = objPageInfo.DirectoryName;
         *
         *  while (source.Contains("<asp:ScriptReference"))
         *  {
         *      /-*<asp:ScriptReference Path="Scripts/Common.js" />*-/
         *      string key = StringFunctions.AllBetween(source,"<asp:ScriptReference",">",true);
         *      string script = StringFunctions.AllBetween(key,"Path=\"","\"");
         *      objScripts.Add(script);
         *      source = source.Replace(key, "");
         *  }
         *
         *
         *  foreach (string script in objScripts)
         *  {
         *
         *      strBody += File.ReadAllText(directory + "\\" + script.Replace("/", "\\"));
         *  }
         *
         *  strBody = General.Utilities.Web.Compression.WebCompress(strBody, General.Utilities.Web.Compression.EnumContentType.Javascript);
         *
         *  FileStream objStream = new FileStream(strTargetFile, FileMode.Create);
         *  StreamWriter objWriter = new StreamWriter(objStream);
         *  objWriter.Write(strBody);
         *  objWriter.Close();
         *  objWriter.Dispose();
         *  objStream.Close();
         *  objStream.Dispose();
         *
         *
         *  return new FileInfo(strTargetFile);
         *
         * }
         */
        #endregion

        #region GetPageKey
        public static string GetPageKey(System.Web.UI.ScriptManager objScriptManager)
        {
            string strPage = objScriptManager.Page.Request.ServerVariables["URL"];

            strPage = StringFunctions.ReplaceCharacters(strPage, new string[] { "/", "\\", "." }, "_");
            if (StringFunctions.StartsWith(strPage, "_"))
            {
                strPage = StringFunctions.AllAfter(strPage, 0);
            }

            return(strPage);
        }
Example #4
0
        private static bool HashMatch(System.Web.UI.ScriptManager objScriptManager)
        {
            //General.Debug.Trace("Starting Hash Match");
            string strPage       = GetPageKey(objScriptManager);
            string strTargetFile = objScriptManager.Page.MapPath("Scripts/Compiled/" + strPage + ".js");
            string strData       = File.ReadAllText(strTargetFile);

            strData = StringFunctions.AllBetween(strData, "<HASH>", "</HASH>");
            if (StringFunctions.IsNullOrWhiteSpace(strData))
            {
                return(false);
            }

            string[] aryHashKeys = strData.Split(',');
            if (aryHashKeys.Length != objScriptManager.Scripts.Count)
            {
                return(false);
            }

            foreach (string strHashKey in aryHashKeys)
            {
                string[] aryTemp = strHashKey.Split(':');
                string   strFile = aryTemp[0];

                if (!AlwaysCompress)
                {
                    System.Web.UI.ScriptReference objRef = FindScriptReference(strFile, objScriptManager);

                    if (objRef == null)
                    {
                        return(false);
                    }

                    bool blnCompressed = General.Data.SqlConvert.ToBoolean(aryTemp[2]);
                    if (blnCompressed != IsCompressed(strFile, objScriptManager))
                    {
                        return(false);
                    }
                }

                string strOldHash = aryTemp[1];
                string strNewHash = GetHashString(objScriptManager.Page.MapPath(strFile));

                if (strOldHash != strNewHash)
                {
                    return(false);
                }
            }
            //General.Debug.Trace("Finished Hash Match");
            return(true);
        }
Example #5
0
 protected void Application_Start(object sender, EventArgs e)
 {
     //if (applicationStartupComplete) return;
     try
     {
         RegisterRoutes(RouteTable.Routes);
         object osm = new System.Web.UI.ScriptManager();
     }
     catch (Exception)
     { }
 }
Example #6
0
 public static System.Collections.ObjectModel.ReadOnlyCollection <System.Web.UI.ServiceReference> GetServiceReferences(System.Web.UI.ScriptManager scriptManager, System.Collections.Generic.IEnumerable <System.Web.UI.ScriptManagerProxy> proxies)
 {
     throw null;
 }
Example #7
0
 public static string GetProxyUrl(System.Web.UI.ScriptManager scriptManager, System.Web.UI.ServiceReference serviceReference)
 {
     throw null;
 }
Example #8
0
 public static string GetApplicationServices(System.Web.UI.ScriptManager scriptManager, System.Collections.Generic.IEnumerable <System.Web.UI.ScriptManagerProxy> proxies)
 {
     throw null;
 }
Example #9
0
        public static string RenderUserControl(string path, bool useFormLess,
            Dictionary<string, string> controlParams, string assemblyName, string controlName,
            HttpContext context)
        {
            System.Web.UI.Page pageHolder = null;
            if (useFormLess)
            {
                pageHolder = new FormlessPage() { AppRelativeTemplateSourceDirectory = HttpRuntime.AppDomainAppVirtualPath }; //needed to resolve "~/"
            }
            else
            {
                pageHolder = new System.Web.UI.Page() { AppRelativeTemplateSourceDirectory = HttpRuntime.AppDomainAppVirtualPath };
            }

            System.Web.UI.UserControl viewControl = null;

            //use path by default
            if (String.IsNullOrEmpty(path))
            {
                //load assembly and usercontrol when .ascx is compiled into a .dll
                string controlAssemblyName = string.Format("{0}.{1},{0}", assemblyName, controlName);

                Type type = Type.GetType(controlAssemblyName);
                viewControl = (System.Web.UI.UserControl)pageHolder.LoadControl(type, null);
            }
            else
            {
                viewControl = (System.Web.UI.UserControl)pageHolder.LoadControl(path);

            }

            pageHolder.EnableViewState = false;
            viewControl.EnableViewState = false;

            if (controlParams != null && controlParams.Count > 0)
            {
                foreach (var keyValuePair in controlParams)
                {
                    Type viewControlType = viewControl.GetType();
                    System.Reflection.PropertyInfo property =
                       viewControlType.GetProperty(keyValuePair.Key);

                    if (property != null)
                    {

                        object value;
                        DateTime date;
                        if (property.PropertyType == typeof(bool))
                            value = bool.Parse(keyValuePair.Value);
                        else if (property.PropertyType == typeof(Int32))
                            value = Int32.Parse(keyValuePair.Value);
                        else if (property.PropertyType == typeof(Guid))
                            value = new Guid(keyValuePair.Value);
                        else if (property.PropertyType == typeof(DateTime) && DateTime.TryParse(keyValuePair.Value, out date))
                            value = date;
                        else
                            value = keyValuePair.Value;

                        try
                        {
                            property.SetValue(viewControl, value, null);
                        }
                        catch (Exception ex)
                        {
                            //need to hook into external logger, throw?
                        }

                    }

                }
            }

            string parseIndex = System.Guid.NewGuid().ToString();
            if (useFormLess)
            {
                pageHolder.Controls.Add(viewControl);
            }
            else
            {
                System.Web.UI.HtmlControls.HtmlForm form = new System.Web.UI.HtmlControls.HtmlForm();
                System.Web.UI.ScriptManager sm = new System.Web.UI.ScriptManager();
                sm.EnableCdn = true;
                sm.AjaxFrameworkMode = System.Web.UI.AjaxFrameworkMode.Disabled;
                System.Web.UI.WebControls.Literal litParseIndex = new System.Web.UI.WebControls.Literal();
                litParseIndex.Text = parseIndex;
                form.Controls.Add(sm);
                form.Controls.Add(litParseIndex);
                form.Controls.Add(viewControl);
                pageHolder.Controls.Add(form);
            }
            System.IO.StringWriter output = new System.IO.StringWriter();
            context.Server.Execute(pageHolder, output, false);

            string renderedContent = output.ToString();

            if (renderedContent.Contains("<form method=")) //have to have a form and scriptmananger to render sometimes but we don't want it on the client
            {
                renderedContent = renderedContent.Substring(renderedContent.IndexOf(parseIndex) + parseIndex.Length);
                renderedContent = renderedContent.Replace(renderedContent.Substring(renderedContent.LastIndexOf("</form>"), 7), "");
            }
            return renderedContent;
        }
Example #10
0
        public static string RenderUserControl(string path, bool useFormLess,
                                               Dictionary <string, string> controlParams, string assemblyName, string controlName,
                                               HttpContext context)
        {
            System.Web.UI.Page pageHolder = null;
            if (useFormLess)
            {
                pageHolder = new FormlessPage()
                {
                    AppRelativeTemplateSourceDirectory = HttpRuntime.AppDomainAppVirtualPath
                };                                                                                                            //needed to resolve "~/"
            }
            else
            {
                pageHolder = new System.Web.UI.Page()
                {
                    AppRelativeTemplateSourceDirectory = HttpRuntime.AppDomainAppVirtualPath
                };
            }

            System.Web.UI.UserControl viewControl = null;

            //use path by default
            if (String.IsNullOrEmpty(path))
            {
                //load assembly and usercontrol when .ascx is compiled into a .dll
                string controlAssemblyName = string.Format("{0}.{1},{0}", assemblyName, controlName);

                Type type = Type.GetType(controlAssemblyName);
                viewControl = (System.Web.UI.UserControl)pageHolder.LoadControl(type, null);
            }
            else
            {
                viewControl = (System.Web.UI.UserControl)pageHolder.LoadControl(path);
            }

            pageHolder.EnableViewState  = false;
            viewControl.EnableViewState = false;

            if (controlParams != null && controlParams.Count > 0)
            {
                foreach (var keyValuePair in controlParams)
                {
                    Type viewControlType = viewControl.GetType();
                    System.Reflection.PropertyInfo property =
                        viewControlType.GetProperty(keyValuePair.Key);

                    if (property != null)
                    {
                        object   value;
                        DateTime date;
                        if (property.PropertyType == typeof(bool))
                        {
                            value = bool.Parse(keyValuePair.Value);
                        }
                        else if (property.PropertyType == typeof(Int32))
                        {
                            value = Int32.Parse(keyValuePair.Value);
                        }
                        else if (property.PropertyType == typeof(Guid))
                        {
                            value = new Guid(keyValuePair.Value);
                        }
                        else if (property.PropertyType == typeof(DateTime) && DateTime.TryParse(keyValuePair.Value, out date))
                        {
                            value = date;
                        }
                        else
                        {
                            value = keyValuePair.Value;
                        }

                        try
                        {
                            property.SetValue(viewControl, value, null);
                        }
                        catch (Exception ex)
                        {
                            //need to hook into external logger, throw?
                        }
                    }
                }
            }

            string parseIndex = System.Guid.NewGuid().ToString();

            if (useFormLess)
            {
                pageHolder.Controls.Add(viewControl);
            }
            else
            {
                System.Web.UI.HtmlControls.HtmlForm form = new System.Web.UI.HtmlControls.HtmlForm();
                System.Web.UI.ScriptManager         sm   = new System.Web.UI.ScriptManager();
                sm.EnableCdn         = true;
                sm.AjaxFrameworkMode = System.Web.UI.AjaxFrameworkMode.Disabled;
                System.Web.UI.WebControls.Literal litParseIndex = new System.Web.UI.WebControls.Literal();
                litParseIndex.Text = parseIndex;
                form.Controls.Add(sm);
                form.Controls.Add(litParseIndex);
                form.Controls.Add(viewControl);
                pageHolder.Controls.Add(form);
            }
            System.IO.StringWriter output = new System.IO.StringWriter();
            context.Server.Execute(pageHolder, output, false);

            string renderedContent = output.ToString();

            if (renderedContent.Contains("<form method=")) //have to have a form and scriptmananger to render sometimes but we don't want it on the client
            {
                renderedContent = renderedContent.Substring(renderedContent.IndexOf(parseIndex) + parseIndex.Length);
                renderedContent = renderedContent.Replace(renderedContent.Substring(renderedContent.LastIndexOf("</form>"), 7), "");
            }
            return(renderedContent);
        }
Example #11
0
        public static FileInfo CompileClientScript(System.Web.UI.ScriptManager objScriptManager)
        {
            _objLog.Clear();
            ArrayList objScripts        = new ArrayList();
            string    strBody           = String.Empty;
            string    strNoCompressBody = String.Empty;
            string    strHeader         = "/*DO NOT REMOVE!! THIS CODE IS USED FOR VERSION CHECKING::: <HASH>";
            string    strPage           = GetPageKey(objScriptManager);
            string    strTargetFolder   = objScriptManager.Page.MapPath("Scripts/Compiled/");
            string    strTargetFile     = strTargetFolder + strPage + ".js";



            foreach (System.Web.UI.ScriptReference objRef in objScriptManager.Scripts)
            {
                string strFile = objScriptManager.Page.MapPath(objRef.Path);
                strHeader += objRef.Path + ":" + GetHashString(strFile) + ":" + IsCompressed(objRef) + ",";

                if (AlwaysCompress)
                {
                    strBody += File.ReadAllText(strFile) + "\r\n\r\n";
                }
                else
                {
                    if (!IsCompressed(objRef))
                    {
                        strNoCompressBody += File.ReadAllText(strFile) + "\r\n\r\n";
                    }
                    else
                    {
                        strBody += File.ReadAllText(strFile) + "\r\n\r\n";
                    }
                }
            }
            if (StringFunctions.Right(strHeader, 1) == ",")
            {
                strHeader = StringFunctions.Shave(strHeader, 1); //Remove last comma
            }
            strHeader += "</HASH>*/";

            //Compression DISABLED 7/2014 for being buggy!!!
            //strBody = General.Utilities.Web.Compression.WebCompress(strBody, General.Utilities.Web.Compression.EnumContentType.Javascript);
            strBody = strNoCompressBody + strBody;
            //_objLog.Write(General.Utilities.Web.Compression.Log.ToString() + StringFunctions.NewLine);
            if (!System.IO.Directory.Exists(strTargetFolder))
            {
                Directory.CreateDirectory(strTargetFolder);
            }
            FileStream   objStream = new FileStream(strTargetFile, FileMode.Create);
            StreamWriter objWriter = new StreamWriter(objStream);

            objWriter.Write(strHeader);
            objWriter.Write(strBody);
            objWriter.Close();
            objWriter.Dispose();
            objStream.Close();
            objStream.Dispose();

            try
            {
                //General.Debugging.Report.SendDebug("Compiling Javascript File: " + strTargetFile, Log.ToString());
            }
            catch { }

            return(new FileInfo(strTargetFile));
        }
Example #12
0
        private static System.Web.UI.ScriptReference FindScriptReference(string strFile, System.Web.UI.ScriptManager objScriptManager)
        {
            foreach (System.Web.UI.ScriptReference objRef in objScriptManager.Scripts)
            {
                if (objRef.Path == strFile)
                {
                    return(objRef);
                }
            }

            return(null);
        }
Example #13
0
 private static bool IsCompressed(string strFile, System.Web.UI.ScriptManager objScriptManager)
 {
     return(IsCompressed(FindScriptReference(strFile, objScriptManager)));
 }