我试图让 bundle 在 ASP.NET MVC 4 中工作。我从为 bundle 的 CSS 生成的链接中收到 404 错误。我做了以下事情:
通过 NuGet (v4.0.20710.0) 安装了“Microsoft ASP.NET Web 优化框架”包
在 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" )); } } }
在站点根目录的 Web.config 中添加了以下内容:
<system.web> <compilation debug="false" targetFramework="4.5" /> <pages> <namespaces> <add namespace="System.Web.Optimization"/> ... </namespaces> </pages> </system.web>
将以下内容添加到我的 MVC 布局文件的 head 元素中:
@Styles.Render("~/bundles/styles/cvi")
将 BundleConfig 中引用的 CSS 文件(“mainstyles.css”)复制到我的 Web 项目的根目录中。
当我查看渲染文件的源代码时,我可以看到链接显示为:
<link href="/bundles/styles/cvi" rel="stylesheet"/>
当浏览到该链接或在 Chrome 的网络选项卡中查看页面请求时,该链接会导致 404。
我也尝试过在 Web 表单上进行等效操作,但我从添加时生成的链接得到了相同的结果 (404):
<%: Styles.Render("~/bundles/styles/cvi") %>
最佳答案
通过 google 结果发现了这个问题,但我的问题是 Windows 2008 在 web.config
中需要这个才能在编译 debug=false 时工作。
<system.webServer>
<modules>
<add name="BundleModule" type="System.Web.Optimization.BundleModule" />
</modules>
</system.webServer>
没有这个它在 Win7 开发机器上工作正常。
关于c# - bundle 的 css 链接出现 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19769662/