【问题标题】:MVC Bundling - Failed to load resourceMVC 捆绑 - 无法加载资源
【发布时间】:2013-08-27 17:03:18
【问题描述】:

这可能是什么原因造成的?我正在使用在本地构建和运行的 DurandalJS 项目。当我尝试部署到 Azure 网站时,应用程序发布但在浏览器中失败:

加载资源失败:服务器响应状态为 404 (未找到) http://appsite.azurewebsites.net/Scripts/vendor.js?v=KJCisWMhJYMzhLi_jWQXizLj9vHeNGfm_1c-uTOfBOM1

我没有自定义任何捆绑。

我的捆绑配置:

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

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

DurandalBundleConfig:

public class DurandalBundleConfig {
public static void RegisterBundles(BundleCollection bundles) {
  bundles.IgnoreList.Clear();
  AddDefaultIgnorePatterns(bundles.IgnoreList);

  bundles.Add(
    new ScriptBundle("~/Scripts/vendor.js")
        .Include("~/Scripts/jquery-{version}.js")
        .Include("~/Scripts/jquery-ui-{version}.js")
        .Include("~/Scripts/bootstrap.js")
        .Include("~/Scripts/knockout-{version}.js")
        .Include("~/Scripts/knockout.mapping-latest.js")
         .Include("~/Scripts/isotope.js")
          .Include("~/Scripts/toastr.js")
          .Include("~/Scripts/tag-it.js")
    );

  bundles.Add(
    new StyleBundle("~/Content/css")
      .Include("~/Content/ie10mobile.css")
      .Include("~/Content/app.css")
      .Include("~/Content/bootstrap.min.css")
      .Include("~/Content/bootstrap-responsive.min.css")
      .Include("~/Content/font-awesome.min.css")
      .Include("~/Content/durandal.css")
      .Include("~/Content/starterkit.css")
       .Include("~/Content/toastr.css")
       .Include("~/Content/tag-it.css")
       .Include("~/Content/zen-theme.css")
    );
}

public static void AddDefaultIgnorePatterns(IgnoreList ignoreList) {
  if(ignoreList == null) {
    throw new ArgumentNullException("ignoreList");
  }

  ignoreList.Ignore("*.intellisense.js");
  ignoreList.Ignore("*-vsdoc.js");
  ignoreList.Ignore("*.debug.js", OptimizationMode.WhenEnabled);
  //ignoreList.Ignore("*.min.js", OptimizationMode.WhenDisabled);
  //ignoreList.Ignore("*.min.css", OptimizationMode.WhenDisabled);
}
} 

在我的 html 页面中,我使用了这个:

@Scripts.Render("~/Scripts/vendor.js")

这会阻止加载所需的库(如淘汰赛)。可能是我的 web.comfig 中的某些内容吗?任何提示都会很棒。

更新:

如果我执行以下操作,我可以在本地重现确切的问题:

new ScriptBundle("~/Scripts/vvendor.js")  // change the virtual output directory here

我认为这意味着视图在发布时由于某种原因找不到目录...

【问题讨论】:

    标签: asp.net-mvc azure bundler durandal azure-web-app-service


    【解决方案1】:

    我遇到了类似的错误。

    在我的例子中,我有一个名为 bundles.Add(new ScriptBundle("~/js") 的包,它在发布时没有创建 javascript 包。

    然后我改成bundles.Add(new ScriptBundle("~/js/main"),它成功了!这很奇怪,但它奏效了。但我不确定确切的根本问题是什么。

    【讨论】:

      【解决方案2】:

      1。不要在包名称中使用文件扩展名

      正如Brett 所提到的,您的捆绑包名称中不应包含文件扩展名。

      这样做的原因是 IIS 路由将假定请求是针对文件如果存在扩展名。

      2。不要使用文件夹路径作为包名

      注意:这不会发生在开发中,因此可能只会在您部署到生产环境时对您造成影响

      要避免的第二件事是捆绑名称与您的项目/网站中的真实文件夹的名称相匹配。 IIS 在使用捆绑路由之前会匹配一个物理文件夹。

      例如如果您有一个用于登录页面样式的 /Content/Login 文件夹,则不能使用名为 ~/Content/Login 的包。 IIS 将看到物理文件夹并假定它是一个目录浏览请求(您可能会从 Azure 服务器收到 403 错误)。

      建议命名:

      建议您在所有包中使用名称bundle,以避免目录冲突,并包括css。 在您的项目中永远不要有一个名为 bundles 的文件夹(因为上面的问题 2)。

      示例包名称:

      • “~/bundles/jquery”
      • “~/bundles/addins”
      • “~/bundles/css”
      • “~/bundles/css/login”
      • “~/bundles/css/bootstrap”

      等

      【讨论】:

        【解决方案3】:

        我昨天遇到了同样的问题,并找到了解决方法。 但它涉及摆脱捆绑。必须有更好的方法来强制 Azure 使用 js 包,但还没有找到(目前)。

        这里是解决方法:检查哪些 js 文件包含在 DurandalBundleConfig.cs 的 scriptbundle 中,并将这些文件包含在 Index.cshtml 中:

        @*@Scripts.Render("~/Scripts/vendor")*@
        <script type="text/javascript" src="~/Scripts/jquery-1.9.1.js"></script>
        <script type="text/javascript" src="~/Scripts/bootstrap.js"></script>
        <script type="text/javascript" src="~/Scripts/knockout-2.3.0.js"></script>
        

        【讨论】:

          【解决方案4】:

          去掉 ScriptBundle 名称上的“.js”部分。出于某种原因,这会导致一系列问题。应该是这样的:

          ...
          new ScriptBundle("~/Scripts/vendor")
          ...
          

          然后像这样将它添加到您的页面中:

          @Scripts.Render("~/Scripts/vendor")
          

          【讨论】:

            【解决方案5】:

            您使用的是哪个版本的网页优化?有关最新优化库的信息,请参阅此Link。更新到 1.1 版后,我遇到了重大更改

            【讨论】:

              猜你喜欢
              • 2016-12-17
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2013-08-15
              • 1970-01-01
              • 1970-01-01
              • 2023-03-08
              • 2023-03-03
              相关资源
              最近更新 更多