Ejemplo n.º 1
0
        internal string GetContentFromHttpContext(HttpContextBase httpContext, ControllerContext controllerContext)
        {
            var urlHelper = new UrlHelper(controllerContext.RequestContext);

            StringBuilder sb = new StringBuilder();

            foreach (var ses in (from s in MothScriptHelper.Stylesheets.SelectMany(i => i.Items)
                                 group s by s.Category into g
                                 select g.Select(x => x.Filename)))
            {
                var stylesheets = ses.ToList();

                var key = "scripthelper.rendercss." + string.Join("|", stylesheets.ToArray());

                sb.AppendLine(Provider.GetFromCache(key, () =>
                {
                    // hashcode bepalen
                    StringBuilder stylo = MothScriptHelper.GetFileContent(stylesheets, httpContext);

                    var outputFile = Encoding.UTF8.GetBytes(stylo.ToString());
                    var hashcode   = HashingInstance.Hash(outputFile);

                    string url = urlHelper.Content("~/resources/css/");
                    return(string.Format("<link rel=\"stylesheet\" type=\"text/css\" media=\"all\" href=\"{2}?keys={0}&amp;{1}\" />", string.Join("|", stylesheets.ToArray()), hashcode, url));
                }, Provider.CacheDurations.ExternalScript));
            }

            return(sb.ToString());
        }
Ejemplo n.º 2
0
        internal string GetScriptsFromHttpContext(HttpContextBase httpContext, ControllerContext controllerContext)
        {
            var urlHelper = new UrlHelper(new RequestContext(httpContext, controllerContext.RouteData));

            StringBuilder sb = new StringBuilder();

            foreach (var ses in (from s in MothScriptHelper.Scripts.SelectMany(i => i.Items)
                                 group s by s.Category into g
                                 select g.Select(x => x.Filename)))
            {
                var scripts = ses.ToList();

                //var scripts = MothScriptHelper.Scripts[cat].ToList();

                var key = "scripthelper.renderscripts." + string.Join("|", scripts.ToArray());

                sb.AppendLine(Provider.GetFromCache(key, () =>
                {
                    // hashcode bepalen
                    StringBuilder script = MothScriptHelper.GetFileContent(scripts, httpContext);

                    var outputFile = Encoding.UTF8.GetBytes(script.ToString());
                    var hashcode   = HashingInstance.Hash(outputFile);

                    var keySb = new StringBuilder();
                    if (Provider.Enable.ScriptMinification)
                    {
                        string url = urlHelper.Content("~/resources/javascript/");
                        keySb.AppendLine(string.Format("<script src=\"{2}?keys={0}&amp;{1}\"></script>", string.Join("|", scripts.ToArray()), hashcode, url));
                    }
                    else
                    {
                        foreach (var s in scripts)
                        {
                            string url = urlHelper.Content(s);
                            keySb.AppendLine(string.Format("<script src=\"{0}?{1}\"></script>", url, hashcode));
                        }
                    }
                    return(keySb.ToString());
                }, Provider.CacheDurations.ExternalScript));
            }

            sb.AppendLine(RenderDataUriFallback(httpContext, controllerContext));

            if (MothScriptHelper.InlineScripts.Any())
            {
                var isb = new StringBuilder("<script>");
                foreach (var s in MothScriptHelper.InlineScripts)
                {
                    isb.AppendLine(s);
                }
                isb.Append("</script>");

                sb.Append(isb.ToString());
            }

            return(sb.ToString());
        }