Example #1
0
        public static MvcHtmlString RenderCssBundle(this HtmlHelper html, string bundlePath, BundleOptions options = BundleOptions.Minified, string media = null)
        {
            if (string.IsNullOrEmpty(bundlePath))
            {
                return(MvcHtmlString.Empty);
            }

            if (!CachePaths())
            {
                BundleCache.SafeClear();
            }

            return(BundleCache.GetOrAdd(bundlePath, str =>
            {
                var filePath = MapPath(bundlePath);

                var baseUrl = VirtualPathUtility.GetDirectory(bundlePath);

                if (options == BundleOptions.Combined)
                {
                    return html.Css(bundlePath.Replace(".bundle", ""), media, options);
                }
                if (options == BundleOptions.MinifiedAndCombined)
                {
                    return html.Css(bundlePath.Replace(".css.bundle", ".min.css"), media, options);
                }

                var cssFiles = File.ReadAllLines(filePath);

                var styles = new StringBuilder();
                foreach (var file in cssFiles)
                {
                    if (file.StartsWith("#"))
                    {
                        continue;
                    }

                    var cssFile = file.Trim()
                                  .Replace(".less", ".css")
                                  .Replace(".sass", ".css")
                                  .Replace(".scss", ".css")
                                  .Replace(".styl", ".css");
                    var cssSrc = Path.Combine(baseUrl, cssFile);

                    styles.AppendLine(
                        html.Css(cssSrc, media, options).ToString()
                        );
                }

                return styles.ToString().ToMvcHtmlString();
            }));
        }