0

Likely a few folks have seen this code before as a way to get around Sitecore not supporting MVC sections, so we can place CSS and JS in the right place on our page:

public static class RazorExtensions {
    public static IHtmlString RenderScripts(this HtmlHelper htmlHelper) {
        List<HelperResult> templates = (from object key in htmlHelper.ViewContext.HttpContext.Items.Keys
            where key.ToString().StartsWith("_script_")
            select htmlHelper.ViewContext.HttpContext.Items[key]).OfType<Func<object, HelperResult>>().Select(template => template(null)).ToList();

        foreach (HelperResult template in templates) {
            htmlHelper.ViewContext.Writer.Write(template);
        }

        return MvcHtmlString.Empty;
    }

    public static MvcHtmlString Script(this HtmlHelper htmlHelper, Func<object, HelperResult> template) {
        htmlHelper.ViewContext.HttpContext.Items["_script_" + Guid.NewGuid()] = template;
        return MvcHtmlString.Empty;
    }

    public static IHtmlString RenderStyles(this HtmlHelper htmlHelper) {
        List<HelperResult> templates = (from object key in htmlHelper.ViewContext.HttpContext.Items.Keys
            where key.ToString().StartsWith("_style_")
            select htmlHelper.ViewContext.HttpContext.Items[key]).OfType<Func<object, HelperResult>>().Select(template => template(null)).ToList();

        foreach (HelperResult template in templates) {
            htmlHelper.ViewContext.Writer.Write(template);
        }

        return MvcHtmlString.Empty;
    }

    public static MvcHtmlString Style(this HtmlHelper htmlHelper, Func<object, HelperResult> template) {
        htmlHelper.ViewContext.HttpContext.Items["_style_" + Guid.NewGuid()] = template;
        return MvcHtmlString.Empty;
    }
}

What I'm trying to do is use the MS optimization bundle in here, but the type appears to be incompatible when I use Styles.Render("") for example. I tried Styles.Render("").ToHtmlString() but it didn't like that either. Has anyone gotten this to work, or have another method that works? I assume nothing changed in Sitecore 9 related to using MVC sections!

  • we have bundles.Add(new StyleBundle("~/bundles/someStyles").Include( "~/path css 1", "~/path css 2", ...) and Styles.RenderFormat("<link href=\"{0}\" rel=\"stylesheet\"/>", "~/bundles/someStyles"). It works fine for us – Anton Mar 12 '18 at 10:50
  • Anton, I haven't tried it yet, but that sounds like it'd work. Can you post it as an answer and if it works, I'll check it off so I can give you credit? – Ken McAndrew Mar 12 '18 at 15:21
2

What we have in Global.asax (could be added to Initialize pipeline):

BundleTable.Bundles..Add(new StyleBundle("~/bundles/someStyles").Include(
            "~/path to css 1",
            "~/path to css 3",
            "...."));

Then we add bundled styles to layout in a next way:

@Styles.RenderFormat("<link href=\"{0}\" rel=\"stylesheet\"/>", "~/bundles/someStyles")

It works fine for us

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.