25

我正在尝试让捆绑在 ASP.NET MVC 4 中工作。我从为捆绑的 CSS 生成的链接中收到 404 错误。我做了以下事情:

  1. 通过 NuGet (v4.0.20710.0) 安装“Microsoft ASP.NET Web 优化框架”包

  2. 在 App_Start 目录中创建了一个 BundleConfig 类,其内容如下:

    using System.Web.Optimization;
    namespace BsdAppTemplate.Web_Nancy.App_Start
    {
        public class BundleConfig
        {
            public static void RegisterBundles(BundleCollection bundles)
            {
                bundles.Add(new StyleBundle("~/bundles/styles/cvi").Include(
                    "~/mainstyles.css"
                ));
            }
        }
    }
    
  3. 在站点根目录的 Web.config 中添加了以下内容:

    <system.web>
        <compilation debug="false" targetFramework="4.5" />
    
        <pages>
          <namespaces>
            <add namespace="System.Web.Optimization"/>
            ...
          </namespaces>
        </pages>
    </system.web>
    
  4. 在我的 MVC 布局文件的 head 元素中添加了以下内容:

     @Styles.Render("~/bundles/styles/cvi")
    
  5. 将 BundleConfig ("mainstyles.css") 中引用的 CSS 文件复制到我的 Web 项目的根目录中。

当我查看渲染文件的源时,我可以看到链接显示为:

<link href="/bundles/styles/cvi" rel="stylesheet"/>

浏览此链接或在 Chrome 的网络选项卡中查看页面请求时,此链接会导致 404。

我也在 Web 表单上尝试了等效的方法,但是我从添加时生成的链接中得到了相同的结果 (404):

<%: Styles.Render("~/bundles/styles/cvi") %>
4

7 回答 7

64

通过谷歌结果找到了这个问题,但在我的情况下,问题是 Windows 2008web.config在编译调试 = false 时需要这个才能工作。

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

没有这个,它在 Win7 开发机器上运行良好。

于 2014-04-02T03:10:38.593 回答
27

您似乎错过了通过调用应用配置的RegisterBundles步骤Application_Start

protected void Application_Start()
{
    ...
    BundleConfig.RegisterBundles(BundleTable.Bundles);
    ...
}

通常在BundleConfig类已经存在的情况下(作为项目模板的一部分或在安装期间由 NuGet 包创建)这个调用也已经存在 - 这就是为什么许多教程都隐含它的原因。

您还应该知道,BundleConfig该类用于分离关注点并保持Application_Start清洁。在简单的情况下,没有什么可以阻止您直接在以下位置注册捆绑包Application_Start

protected void Application_Start()
{
    ...
    BundleTable.Bundles.Add(new StyleBundle("~/bundles/styles/cvi").Include("~/mainstyles.css"));

    ...
}
于 2013-11-04T18:39:07.903 回答
13

I had the same problem that my script bundle suddenly responded with 404. I a solution similar to @fiat answer that I found on this blogpost.

The solution was to remove and add the BundleModule in the modules part section of the system.webServer section.

<modules runAllManagedModulesForAllRequests="true">
    <remove name="BundleModule" />
    <add name="BundleModule" type="System.Web.Optimization.BundleModule" />
</modules>
于 2014-08-29T09:21:28.807 回答
5

我遇到了同样的问题(在 ASP.Net webform 中),我通过忽略 Global.asax 中的“bundles/”路由解决了我的问题:

routeCollection.Ignore("bundles/{*catch}");
于 2016-09-03T05:33:25.303 回答
2

如果您在使用 Umbraco 时遇到此错误,请不要忘记将此行添加到您的 web.config:

<add key="Umbraco.Core.ReservedUrls" value="~/bundles/" />
于 2020-06-14T23:01:30.507 回答
0

Figured I'll share what happened to me, when working on my machine I was running with "debug=false" so each individual file was loaded but when pushing to stage/prod I got 404's from the /bundles/styles.

Turns out I has a rewrite rule that was forcing a trailing slash so a request to

/bundles/styles was redirected to /bundle/styles/ which does not exist. Added a exception for the rewrite and now things works just fine.

于 2021-03-21T17:33:41.253 回答
0

在我的 WebAPI 中找不到所有以“~/bundles/...”开头的包名称。将该行添加到我的 RouteConfig.cs 类

    routeCollection.Ignore("bundles/{*catch}");

解决了我的问题。

于 2019-02-11T23:40:59.267 回答