捆绑的css链接收到404错误

时间:2013-11-04 14:18:01

标签: c# asp.net asp.net-mvc asp.net-mvc-4 bundle

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

  1. 通过NuGet安装“Microsoft ASP.NET Web Optimization Framework”软件包(v4.0.20710.0)

  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项目的根目录中。

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

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

    此链接在浏览时会生成404,或在Chrome的网络标签中查看网页请求。

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

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

7 个答案:

答案 0 :(得分:60)

通过谷歌搜索结果找到了这个问题,但我的案例中的问题是在编译debug = false时,{2008}需要web.config中的这个问题。

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

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

答案 1 :(得分:26)

您似乎错过了通过调用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"));

    ...
}

答案 2 :(得分:13)

我有同样的问题,我的脚本包突然响应404.我的解决方案类似于我在this blogpost找到的@fiat回答。

解决方案是在BundleModule部分的模块部分部分删除并添加system.webServer

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

答案 3 :(得分:4)

我有同样的问题(在ASP.Net webform中),我用Ignore&#34; bundles /&#34;解决了我的问题。 Global.asax中的路线:

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

答案 4 :(得分:2)

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

<add key="Umbraco.Core.ReservedUrls" value="~/bundles/" />

答案 5 :(得分:0)

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

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

解决了我的问题。

答案 6 :(得分:0)

我想我会分享发生在我身上的事情,当我在我的机器上工作时,我使用“debug=false”运行,所以每个单独的文件都被加载,但是当推送到 stage/prod 时,我从 /bundles/styles 得到了 404 .

原来我有一个重写规则,它强制使用尾部斜杠,因此请求

/bundles/styles 被重定向到不存在的 /bundle/styles/。为重写添加了一个例外,现在一切正常。

相关问题