【问题标题】:Sitecore 6.6, MVC 3 and System.Web.Optimization?Sitecore 6.6、MVC 3 和 System.Web.Optimization?
【发布时间】:2012-12-01 18:54:21
【问题描述】:

Sitecore 还不支持 MVC 4,我想使用 System.Web.Optimization 的捆绑和缩小。

对捆绑包的请求以 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>

包的路径是虚拟的,不映射到物理文件夹。

忽略路由会引发 NotImplementedException 和 500 Internal Server Error:

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

.. 但除此之外,请求由 Sitecore 处理并以 404 Not Found + Redirect 响应。

我也试过了:

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

我无法让这一切一起工作。救命!

【问题讨论】:

  • 我担心 Sitecore 可能会在处理捆绑包之前定义一条捕获所有路由...

标签: asp.net-mvc sitecore bundle minify


【解决方案1】:

上帝之母!

绕过以下路线:

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

routes.MapRoute(
    "sc_ignore_Bundles_Js",
    "bundles/{*pathInfo}"
);

【讨论】:

  • 您是否需要做其他事情才能使其正常工作?我尝试添加路由,但 sitecore 不断劫持捆绑包或内容请求并返回未找到项目错误。
  • 我不再有这个项目了,但据我所知..您需要确保(在某些 .config 文件中)“排除模式”设置为“ sc_ignore_'。
  • 我已将 /content/ 和 /bundle/ 添加到 web.config 中的 ignoreurlprefixes。但听起来你是说我需要添加更多内容?我会在谷歌上搜索 sc_ignore,看看我能找到什么。谢谢!作为旁注。当我将 url 添加到 ignoreurlprefix 时,静态文件处理程序启动,我只是得到一个常规的 404 错误 =\
  • 我是说配置文件中的某个设置(键/值)应该看起来像“sc_ignore_”。当我看到它时,我认为我可以用它来绕过 Sitecore管道。我会尝试搜索(解决方案)“sc_ignore”。
  • 是的,我看到了,我尝试设置它。它与您使用的前缀相同。不幸的是,这似乎没有什么不同。 Sitecore 仍在尝试查找项目“content/css”并进行重定向。
【解决方案2】:

有一个名为“IgnoreUrlPrefixes”的 Sitecore 设置,使用 sitecore 配置包含您可以修补此设置以包含例如“/bundles”,它允许您使用 /bundles/* url 进行 ASP.NET Web 优化捆绑功能。

【讨论】:

  • 使用 Sitecore 7.x 这似乎是唯一需要|/bundles
【解决方案3】:

就我而言,使用 Sitecore 6.6 update 5,我能够通过执行以下操作使捆绑工作:

首先,将其添加到 web.config:

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

其次,我在管道中添加了一个管道方法,用于在捆绑表中注册捆绑包:

[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"));
}

接下来,我通过补丁文件将管道方法添加到管道中:

<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>

最后,我修补了 sitecore 中的 IgnoreUrlPrefixes 设置以添加 /bundles 路径

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

... 不需要其他任何东西 - 像冠军一样工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多