private MvcHtmlString InsertCss_Bundled(transformType ttype)
 {
     var files = new HashSet<string>();
     var bundles = BundleTable.Bundles;
     //			BundleTable.Bundles.Clear();
     IBundleTransform transform = ttype == transformType.Compress ? new CssMinify() : null  as IBundleTransform;
     var vpath = CombineUrlPath(LocalCssPath, BundleFile);
     var bundle = new Bundle(vpath, transform);
     foreach (var lib in Segments.stdFiles)
     {
         string name = lib.Css ?? lib.Name;
         if (!files.Contains(name))
         {
             files.Add(name);
             if (name[0] == '/')
             {
                 bundle.Include(name);
             }
             else
             {
                 var sheet = CssDetails.FirstOrDefault(css => css.Name == name);
                 if (sheet != null)
                     bundle.Include(CombineUrlPath(LocalCssPath, sheet.PathName));
             }
         }
     }
     bundles.Add(bundle);
     //			var resp = bundle.GenerateBundleResponse(new BundleContext(this.httpcontext, bundles, this.LocalCssPath));
     return new MvcHtmlString(
                     String.Format(@"<link href=""{0}"" rel=""stylesheet"" type=""text/css"" />", bundles.ResolveBundleUrl(vpath)));
 }
        private MvcHtmlString InsertScripts_Bundled(transformType ttype)
        {
            var files = new HashSet<string>();
            //			BundleTable.Bundles.Clear();
            IBundleTransform transform = ttype == transformType.Compress ? new JsMinify() : null as IBundleTransform;
            var vpath = CombineUrlPath(LocalJsPath, BundleFile);
            var bundle = new Bundle(vpath, transform);
            BundleTable.Bundles.Add(bundle);
            int cnt = 0;  //Bundles offer no practical way to know how many file it contains, so we have to count them.
            int segment = 0;   // bundling is just broken.   Let's try to workaround it.

            LibraryDetail self = null;
            StringWriter sb = new StringWriter();
            foreach (LibraryDetail lib in Segments.stdFiles)
            {
                string name = lib.Name;
                if (lib.UseGoogle || (lib.PathName !=null && lib.PathName.StartsWith("http")))
                {
                    if(cnt > 0)
                    {
                        this.RenderJavascriptFile(sb, BundleTable.Bundles.ResolveBundleUrl(vpath));
                        vpath = CombineUrlPath(LocalJsPath, BundleFile) + ++segment;
                        bundle = new Bundle(vpath, transform);
                        BundleTable.Bundles.Add(bundle);
                        cnt = 0;
                    }
                    var pathName = lib.UseGoogle ? string.Format(@"http://ajax.googleapis.com/ajax/libs/{0}/{1}/{0}.js", name, lib.Version) :  lib.PathName;
                    this.RenderJavascriptFile(sb, pathName);
                    continue;
                }

                if (lib.Alias.Contains("self"))
                    self = lib;
                else
                {
                    var pathname = lib.PathName;
                    if (this.UseDebugScripts || Debugger.IsAttached)
                        pathname = lib.DebugPathName ?? lib.PathName;
                    if (pathname != null)
                    {
                        bundle.Include(CombineUrlPath(LocalJsPath, pathname));
                        ++cnt;
                    }
                }
            }
            if (self != null && self.PathName != null)
            {
                bundle.Include(CombineUrlPath(LocalJsPath, self.PathName));
                ++cnt;
            }
            foreach (string file in Segments.files)
            {
                bundle.Include(file);
                ++cnt;
            }
            if (cnt >0)
                this.RenderJavascriptFile(sb, BundleTable.Bundles.ResolveBundleUrl(vpath));

            if (Segments.segments.Any())
            {
                this.RenderJavascriptBlocks(sb);
            }
            Segments.wasRendered = true;

            return new MvcHtmlString(sb.GetStringBuilder().ToString());
        }