asp.net-mvc MVC 捆绑 - 无法加载资源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18423253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MVC Bundling - Failed to load resource
提问by RobVious
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:
什么可能导致这种情况?我正在使用一个在本地构建和运行的 DurandalJS 项目。当我尝试部署到 Azure 网站时,该应用程序发布但在浏览器中失败:
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
无法加载资源:服务器响应状态为 404(未找到) 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:
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:
在我的 html 页面中,我使用了这个:
@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.
这会阻止加载所需的库(如淘汰赛)。它可能是我的 web.comfig 中的东西吗?任何提示都会很棒。
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...
我认为这意味着视图在由于某种原因发布时找不到目录......
回答by Gone Coding
1. Do not use file extensions in bundle names
1. 不要在包名中使用文件扩展名
As Brettmentions, you should not have a file extension in your bundle name.
正如Brett 所提到的,您的包名称中不应包含文件扩展名。
The reason for this is that IIS routing will assume the request is for a file if an extension is present.
这样做的原因是,如果存在扩展名,IIS 路由将假定请求是针对文件的。
2. Do not use folder paths as bundle names
2. 不要使用文件夹路径作为包名
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 folderbefore it resorts to the bundle routing.
要避免的第二件事是使用与项目/网站中真实文件夹名称匹配的包名称。IIS 将在使用捆绑路由之前匹配物理文件夹。
e.g. if you have a /Content/Loginfolder 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).
例如,如果您有一个/Content/Login用于登录页面样式的文件夹,则不能使用名为~/Content/Login. IIS 将看到物理文件夹并假定它是目录浏览请求(您可能会从 Azure 服务器收到 403 错误)。
Suggested naming:
建议命名:
Suggest you use the name bundlein all bundles, to avoid a directory clash, and include cssas well. Do not ever have a folder called bundlesin your project (because of issue 2 above).
建议您bundle在所有包中使用该名称,以避免目录冲突,并且包括在内css。在您的项目中永远不要有一个名为 bundles 的文件夹(因为上面的问题 2)。
Example bundle names:
示例包名称:
- "~/bundles/jquery"
- "~/bundles/addins"
- "~/bundles/css"
- "~/bundles/css/login"
- "~/bundles/css/bootstrap"
- “~/bundles/jquery”
- “〜/捆绑/插件”
- “~/bundles/css”
- “~/bundles/css/登录”
- “~/bundles/css/bootstrap”
etc
等等
回答by Brett
Get rid of the ".js" part on the ScriptBundle name. For some reason that causes a mess of problems. It should be this:
去掉 ScriptBundle 名称上的“.js”部分。出于某种原因,这会导致一团糟的问题。应该是这样的:
...
new ScriptBundle("~/Scripts/vendor")
...
Then add it to your page like this:
然后将其添加到您的页面,如下所示:
@Scripts.Render("~/Scripts/vendor")
回答by Dhanuka777
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.
就我而言,我有一个名为的包bundles.Add(new ScriptBundle("~/js"),它在发布时没有创建 javascript 包。
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.
然后我改成了bundles.Add(new ScriptBundle("~/js/main"),它奏效了!这很奇怪,但它奏效了。但我不确定确切的潜在问题是什么。
回答by ntombela
回答by MagnusJarl
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).
我昨天遇到了同样的问题,并找到了解决方法。但它涉及摆脱捆绑。必须有更好的方法来强制 Azure 使用 js 包,但还没有找到它(还没有)。
Here is the workaround: Check which js files are included in the scriptbundle in DurandalBundleConfig.cs and included those files on the Index.cshtml:
解决方法如下:检查 DurandalBundleConfig.cs 中的 scriptbundle 中包含哪些 js 文件,并将这些文件包含在 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>

