MVC Bundling - Failed to load resource

2019-01-17 22:56发布

问题:

What could be causing this? I'm working with a DurandalJS project that builds and runs locally just fine. When I try to deploy to an Azure Website, the app publishes but fails in the browser with:

Failed to load resource: the server responded with a status of 404 (Not Found) http://appsite.azurewebsites.net/Scripts/vendor.js?v=KJCisWMhJYMzhLi_jWQXizLj9vHeNGfm_1c-uTOfBOM1

I haven't customized any of the bundling.

my bundle configs:

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

In my html page I use this:

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

This is preventing required libraries (like knockout) from loading. Could it be something in my web.comfig? Any tips would be awesome.

UPDATE:

I can reproduce the exact issue locally if I do the following:

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

I think this means the view can't find the directory when it's published for some reason...

回答1:

Get rid of the ".js" part on the ScriptBundle name. For some reason that causes a mess of problems. It should be this:

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

Then add it to your page like this:

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


回答2:

1. Do not use file extensions in bundle names

As Brett mentions, you should not have a file extension in your bundle name.

The reason for this is that IIS routing will assume the request is for a file if an extension is present.

2. Do not use folder paths as bundle names

Note: This does not occur in development, so may only bite you when you deploy to production

The second thing to avoid is having bundle names that match the name of a real folder in your project/website. IIS will match a physical folder before it resorts to the bundle routing.

e.g. if you have a /Content/Login folder for your login page styles, you cannot use a bundle called ~/Content/Login. IIS will see the physical folder and assume it is a directory browse request (you will likely get a 403 error from Azure servers).

Suggested naming:

Suggest you use the name bundle in all bundles, to avoid a directory clash, and include css as well. Do not ever have a folder called bundles in your project (because of issue 2 above).

Example bundle names:

  • "~/bundles/jquery"
  • "~/bundles/addins"
  • "~/bundles/css"
  • "~/bundles/css/login"
  • "~/bundles/css/bootstrap"

etc



回答3:

I had the similar error.

In my case I had a bundle called bundles.Add(new ScriptBundle("~/js")which didn't create the javascript bundle when published.

Then I changed to bundles.Add(new ScriptBundle("~/js/main") and it worked! It's weird, but it worked. But I am not sure what is the exact underlying issue.



回答4:

Which version of Web Optimization are you using? Refer to this Link for information on the latest optimization library. I encountered breaking changes after updating to version 1.1



回答5:

I experienced the same problem yesterday and found a workaround for it. But it involves getting rid of the bundle. There must be a better way to force Azure to use a js bundle but haven't found it (yet).

Here is the workaround: Check which js files are included in the scriptbundle in DurandalBundleConfig.cs and included those files on the 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>