Skip to content

Instantly share code, notes, and snippets.

@CosAnca
Forked from mariusschulz/HtmlHelperExtensions.cs
Created November 26, 2019 17:55
Show Gist options
  • Save CosAnca/7d1544fa44c24c709728f6856b700963 to your computer and use it in GitHub Desktop.
Save CosAnca/7d1544fa44c24c709728f6856b700963 to your computer and use it in GitHub Desktop.

Revisions

  1. @mariusschulz mariusschulz revised this gist Oct 25, 2015. 1 changed file with 3 additions and 4 deletions.
    7 changes: 3 additions & 4 deletions HtmlHelperExtensions.cs
    Original file line number Diff line number Diff line change
    @@ -17,20 +17,19 @@ public static IHtmlString InlineStyles(this HtmlHelper htmlHelper, string bundle

    private static IHtmlString InlineBundle(this HtmlHelper htmlHelper, string bundleVirtualPath, string htmlTagName)
    {
    string bundleContent = htmlHelper.LoadBundleContent(bundleVirtualPath);
    string bundleContent = LoadBundleContent(htmlHelper.ViewContext.HttpContext, bundleVirtualPath);
    string htmlTag = string.Format("<{0}>{1}</{0}>", htmlTagName, bundleContent);

    return new HtmlString(htmlTag);
    }

    private static string LoadBundleContent(this HtmlHelper htmlHelper, string bundleVirtualPath)
    private static string LoadBundleContent(HttpContextBase httpContext, string bundleVirtualPath)
    {
    var httpContext = htmlHelper.ViewContext.HttpContext;
    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;
    }
    }
    }
  2. @mariusschulz mariusschulz revised this gist Oct 25, 2015. No changes.
  3. @mariusschulz mariusschulz revised this gist Oct 25, 2015. No changes.
  4. @mariusschulz mariusschulz renamed this gist Oct 25, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. @mariusschulz mariusschulz revised this gist Oct 25, 2015. 1 changed file with 12 additions and 0 deletions.
    12 changes: 12 additions & 0 deletions Layout.cshtml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    <!doctype html>
    <html lang="en">
    <head>
    <title>Title</title>
    @Html.InlineStyles("~/some/path/to/a/bundle.css")
    </head>
    <body>
    @RenderBody()

    @Html.InlineScripts("~/some/path/to/a/bundle.js")
    </body>
    </html>
  6. @mariusschulz mariusschulz created this gist Oct 25, 2015.
    36 changes: 36 additions & 0 deletions HtmlHelperExtensions.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    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 = htmlHelper.LoadBundleContent(bundleVirtualPath);
    string htmlTag = string.Format("<{0}>{1}</{0}>", htmlTagName, bundleContent);

    return new HtmlString(htmlTag);
    }

    private static string LoadBundleContent(this HtmlHelper htmlHelper, string bundleVirtualPath)
    {
    var httpContext = htmlHelper.ViewContext.HttpContext;
    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;
    }
    }