Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sitecore 6.6, MVC 3 and System.Web.Optimization?

Sitecore does not support MVC 4 yet and I'd like to use System.Web.Optimization's bundling and minification.

Requests to bundles respond with 404 Not Found.

BundleConfig.cs :

public class BundleConfig
{
    // For more information on Bundling, visit http://go.microsoft.com/fwlink/?LinkId=254725
    public static void RegisterBundles(BundleCollection bundles)
    {
        bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                    "~/Scripts/jquery-{version}.js"));

        bundles.Add(new ScriptBundle("~/bundles/jqueryui").Include(
                    "~/Scripts/jquery-ui-{version}.js"));

        bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
                    "~/Scripts/jquery.unobtrusive*",
                    "~/Scripts/jquery.validate*"));

        // Use the development version of Modernizr to develop with and learn from. Then, when you're
        // ready for production, use the build tool at http://modernizr.com to pick only the tests you need.
        bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
                    "~/Scripts/modernizr-*"));

        bundles.Add(new StyleBundle("~/content/css").Include(
                    "~/Content/site.css",
                    "~/Content/960.gs/960.css"));

        bundles.Add(new StyleBundle("~/Content/themes/base/css").Include(
                    "~/Content/themes/base/jquery.ui.core.css",
                    "~/Content/themes/base/jquery.ui.resizable.css",
                    "~/Content/themes/base/jquery.ui.selectable.css",
                    "~/Content/themes/base/jquery.ui.accordion.css",
                    "~/Content/themes/base/jquery.ui.autocomplete.css",
                    "~/Content/themes/base/jquery.ui.button.css",
                    "~/Content/themes/base/jquery.ui.dialog.css",
                    "~/Content/themes/base/jquery.ui.slider.css",
                    "~/Content/themes/base/jquery.ui.tabs.css",
                    "~/Content/themes/base/jquery.ui.datepicker.css",
                    "~/Content/themes/base/jquery.ui.progressbar.css",
                    "~/Content/themes/base/jquery.ui.theme.css"));
    }
}

_Layout.cshtml :

@using System.Web.Optimization
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>@ViewBag.Title</title>
        @Styles.Render("~/Content/css")
        @Scripts.Render("~/bundles/modernizr")
    </head>
    <body>
        <div class="container_12">
            <a href="/"><h1>Title</h1></a>
            @Html.Action("Utilities", "Navigation")
            @Html.Action("Menu", "Navigation")
            @RenderBody()
        </div>
        @Scripts.Render("~/bundles/jquery")
        @RenderSection("scripts", required: false)
    </body>
</html>

Paths to bundles are virtual and do not map to physical folders.

Ignoring routes throws a NotImplementedException and 500 Internal Server Error:

routes.IgnoreRoute("content/{*pathInfo}");
routes.IgnoreRoute("bundles/{*pathInfo}");

.. but otherwise, requests are handled by Sitecore and respond with a 404 Not Found + Redirect.

I've also tried:

<system.webServer>
    <modules runAllManagedModulesForAllRequests="false">
        <remove name="BundleModule"/>
        <add type="System.Web.Optimization.BundleModule" name="BundleModule"/>
    </modules>

I can't get this all to work together. Help!

like image 597
maxbeaudoin Avatar asked Dec 13 '12 19:12

maxbeaudoin


3 Answers

Mother of GOD!

Hackaround the following routes:

routes.MapRoute(
    "sc_ignore_Bundles_Css",
    "content/{*pathInfo}"
);

routes.MapRoute(
    "sc_ignore_Bundles_Js",
    "bundles/{*pathInfo}"
);
like image 93
maxbeaudoin Avatar answered Oct 22 '22 08:10

maxbeaudoin


There is a Sitecore setting named "IgnoreUrlPrefixes", using a sitecore config include you can patch this setting to include for example "/bundles" which allows you to use the /bundles/* urls for the ASP.NET Web Optimization bundling features.

like image 41
IvanL Avatar answered Oct 22 '22 07:10

IvanL


In my case, using Sitecore 6.6 update 5, I was able to get bundling to work by doing the following:

First, add this to the web.config:

<system.webServer>
<modules runAllManagedModulesForAllRequests="false">
    <remove name="BundleModule"/>
    <add type="System.Web.Optimization.BundleModule" name="BundleModule"/>
</modules>
...

Second, I added a pipeline method to the pipeline to register the bundles in the bundle table:

[UsedImplicitly]
public virtual void Process(PipelineArgs args)
{
    BundleTable.EnableOptimizations = true;            
    RegisterBundles( BundleTable.Bundles );
}

private void RegisterBundles(BundleCollection bundles)
{
    bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                "~/Scripts/jquery-{version}.js"));
}

Next, I added the pipeline method to the pipeline via a patch file:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
   <sitecore>
      <pipelines>
         <initialize>
            <processor patch:before="processor[@type='Sitecore.Mvc.Pipelines.Loader.InitializeGlobalFilters, Sitecore.Mvc']"
               type="MyStuff.Web.Pipelines.RegisterMyBundles, MyStuff.Web" />
         </initialize>
      </pipelines>
   </sitecore>
</configuration>

Finally, I patched the IgnoreUrlPrefixes setting in sitecore to add the /bundles path

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
   <sitecore>
      <settings>
         <setting name="IgnoreUrlPrefixes"
                  value="(all other sitecore paths here)|/bundles"/>
      </settings>
   </sitecore>
</configuration>

... Nothing else was needed - worked like a champ.

like image 26
Brian Beckham Avatar answered Oct 22 '22 06:10

Brian Beckham