Ejemplo n.º 1
0
        public static IHtmlString Script(this HtmlHelper helper, string includeJS)
        {
            if (helper == null)
            {
                throw new ArgumentNullException("helper");
            }
            if (String.IsNullOrEmpty(includeJS))
            {
                throw new ArgumentNullException("includeJS");
            }

            var context = helper.ViewContext.HttpContext;

            var script  = ScriptBundleManager.GetScriptBundle(includeJS);
            var scripts = GetIncludedScripts(context);

            if (!scripts.Contains(script))
            {
                scripts.Add(script);

                return(new HtmlString(String.Format("    <script src=\"{0}\" type=\"text/javascript\"></script>\n",
                                                    HttpUtility.HtmlAttributeEncode(ContentHashCache.ResolveWithHash(script)))));
            }
            else
            {
                return(new HtmlString(""));
            }
        }
Ejemplo n.º 2
0
        public static IHtmlString ScriptBundle(this HtmlHelper helper, string bundleKey)
        {
            if (helper == null)
            {
                throw new ArgumentNullException("helper");
            }

            if (String.IsNullOrEmpty(bundleKey))
            {
                throw new ArgumentNullException("bundleKey");
            }

            var context = helper.ViewContext.HttpContext;

            if (!ScriptBundleManager.IsEnabled)
            {
                StringBuilder sb = new StringBuilder();
                foreach (var include in ScriptBundleManager.GetBundleIncludes(bundleKey))
                {
                    var scriptUrl = include;
                    if (string.IsNullOrEmpty(scriptUrl))
                    {
                        continue;
                    }

                    if (scriptUrl != null && scriptUrl.StartsWith("dynamic://", StringComparison.OrdinalIgnoreCase))
                    {
                        var scriptName = scriptUrl.Substring(10);
                        scriptUrl = DynamicScriptManager.GetScriptInclude(scriptName);
                        scriptUrl = VirtualPathUtility.ToAbsolute("~/DynJS.axd/" + scriptUrl);
                    }
                    else
                    {
                        scriptUrl = BundleUtils.ExpandVersionVariable(scriptUrl);
                        scriptUrl = VirtualPathUtility.ToAbsolute(scriptUrl);
                    }

                    var scripts = GetIncludedScripts(context);

                    if (!scripts.Contains(scriptUrl))
                    {
                        scripts.Add(scriptUrl);
                        sb.AppendLine(String.Format("    <script src=\"{0}\" type=\"text/javascript\"></script>\n",
#if !ASPNETMVC
                                                    WebUtility.HtmlEncode(ContentHashCache.ResolveWithHash(scriptUrl))));
#else
                                                    HttpUtility.HtmlAttributeEncode(ContentHashCache.ResolveWithHash(scriptUrl))));
#endif
                    }
                }

                return(new HtmlString(sb.ToString()));
            }

            return(Script(helper, "dynamic://Bundle." + bundleKey));
        }
Ejemplo n.º 3
0
        private static void Changed(string name)
        {
            var extension = Path.GetExtension(name);

            if (extension == null || string.Compare(extension, ".css", StringComparison.OrdinalIgnoreCase) != 0)
            {
                return;
            }

            ContentHashCache.ScriptsChanged();
            ScriptBundleManager.ScriptsChanged();
        }
Ejemplo n.º 4
0
        public static void ScriptsChanged()
        {
            BundleUtils.ClearVersionCache();
            var instance = ScriptBundleManager.instance;

            if (instance != null &&
                instance.bundleByKey != null)
            {
                foreach (var bundle in instance.bundleByKey.Values)
                {
                    bundle.Changed();
                }
            }

            instance = null;
        }