using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Optimization; public static class HtmlHelperExtensions { public static IHtmlString InlineScripts(this HtmlHelper htmlHelper, string bundleVirtualPath) { return htmlHelper.InlineBundle(bundleVirtualPath, htmlTagName: "script"); } public static IHtmlString InlineStyles(this HtmlHelper htmlHelper, string bundleVirtualPath) { return htmlHelper.InlineBundle(bundleVirtualPath, htmlTagName: "style"); } private static IHtmlString InlineBundle(this HtmlHelper htmlHelper, string bundleVirtualPath, string htmlTagName) { string bundleContent = LoadBundleContent(htmlHelper.ViewContext.HttpContext, bundleVirtualPath); string htmlTag = string.Format("<{0}>{1}", htmlTagName, bundleContent); return new HtmlString(htmlTag); } private static string LoadBundleContent(HttpContextBase httpContext, string bundleVirtualPath) { var bundleContext = new BundleContext(httpContext, BundleTable.Bundles, bundleVirtualPath); var bundle = BundleTable.Bundles.Single(b => b.Path == bundleVirtualPath); var bundleResponse = bundle.GenerateBundleResponse(bundleContext); return bundleResponse.Content; } }