为什么我的 CSS 捆绑不适用于 bin 部署的 MVC4 应用程序? [英] Why is my CSS bundling not working with a bin deployed MVC4 app?

查看:32
本文介绍了为什么我的 CSS 捆绑不适用于 bin 部署的 MVC4 应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经根据此处给出的建议和一两个即时修复将 MVC4 应用程序部署到我的托管服务提供商,但最直接的问题是 css 的捆绑不起作用.当我用显式文件引用替换包引用时,我的 css 再次工作.

I have bin deployed an MVC4 application to my hosting provider, based on advice given here and one or two on-the-fly fixes, but the most immediately apparent problem is that the bundling for css doesn't work. When I replace the bundle ref with explicit file refs, my css works again.

我使用的是来自 VS2012 的标准 MVC4 RTM 项目模板.该提供程序正在运行 IIS 7.5 和 ASP.NET 4,并且我以前的同一应用程序的 MVC3 版本运行良好.我猜我在某个版本太低的地方获取了依赖项,这也可能有助于我的 基于区域的动作链接问题.

I am using a standard MVC4 RTM project template from VS2012. The provider is running IIS 7.5 and ASP.NET 4, and my previous MVC3 version of the same app worked fine. I am guessing I have grabbed a dependency somewhere of too low a version, and this might also contribute to my area based action link problem.

技术症状是:

@Styles.Render("~/Content/css") 呈现为 <link href="/Content/css?v=" rel="stylesheet"/>

推荐答案

无论 .NET 运行的是 4.0 还是 4.5,CSS 和脚本捆绑都应该有效.我正在运行 .NET 4.0,它对我来说很好用.但是,为了使缩小和捆绑行为起作用,您的 web.config 必须设置为不在调试模式下运行.

The CSS and Script bundling should work regardless if .NET is running 4.0 or 4.5. I am running .NET 4.0 and it works fine for me. However in order to get the minification and bundling behavior to work your web.config must be set to not be running in debug mode.

<compilation debug="false" targetFramework="4.0">

以 _Layout.cshtml 文件中的 jQuery UI 示例包为例.

Take this bundle for jQuery UI example in the _Layout.cshtml file.

@Styles.Render("~/Content/themes/base/css")

如果我使用 debug="true" 运行,我会得到以下 HTML.

If I run with debug="true" I get the following HTML.

<link href="/Content/themes/base/jquery.ui.core.css" rel="stylesheet"/>
<link href="/Content/themes/base/jquery.ui.resizable.css" rel="stylesheet"/>
<link href="/Content/themes/base/jquery.ui.selectable.css" rel="stylesheet"/>
<link href="/Content/themes/base/jquery.ui.accordion.css" rel="stylesheet"/>
<link href="/Content/themes/base/jquery.ui.autocomplete.css" rel="stylesheet"/>
<link href="/Content/themes/base/jquery.ui.button.css" rel="stylesheet"/>
<link href="/Content/themes/base/jquery.ui.dialog.css" rel="stylesheet"/>
<link href="/Content/themes/base/jquery.ui.slider.css" rel="stylesheet"/>
<link href="/Content/themes/base/jquery.ui.tabs.css" rel="stylesheet"/>
<link href="/Content/themes/base/jquery.ui.datepicker.css" rel="stylesheet"/>
<link href="/Content/themes/base/jquery.ui.progressbar.css" rel="stylesheet"/>
<link href="/Content/themes/base/jquery.ui.theme.css" rel="stylesheet"/>

但是如果我使用 debug="false" 运行.我会得到这个.

But if I run with debug="false". I'll get this instead.

<link href="/Content/themes/base/css?v=myqT7npwmF2ABsuSaHqt8SCvK8UFWpRv7T4M8r3kiK01" rel="stylesheet"/>

这是一项功能,因此您可以轻松地调试脚本和 CSS 文件的问题.我使用的是 MVC4 RTM.

This is a feature so you can easily debug problems with your Script and CSS files. I'm using the MVC4 RTM.

如果您认为这可能是 MVC 依赖问题,我建议您进入 Nuget 并删除所有与 MVC 相关的包,然后搜索 Microsoft.AspNet.Mvc 包并安装它.我使用的是最新版本,它的版本是 v.4.0.20710.0.这应该会获取您需要的所有依赖项.

If you think it might be an MVC dependency problem, I'd recommend going into Nuget and removing all of your MVC related packages, and then search for the Microsoft.AspNet.Mvc package and install it. I'm using the most recent version and it's coming up as v.4.0.20710.0. That should grab all the dependencies you need.

此外,如果您曾经使用 MVC3 而现在尝试使用 MVC4,您将需要进入您的 web.config(s) 并更新它们的引用以指向 MVC 的 4.0 版本.如果您不确定,您可以随时创建一个新的 MVC4 应用程序并从那里复制 web.config.如果这样做,请不要忘记 Views/Areas 文件夹中的 web.config.

Also if you used to be using MVC3 and are now trying to use MVC4 you'll want to go into your web.config(s) and update their references to point to the 4.0 version of MVC. If you're not sure, you can always create a fresh MVC4 app and copy the web.config from there. Don't forget the web.config in your Views/Areas folders if you do.

更新: 我发现您需要的是安装在您的项目中的 Nuget 包 Microsoft.AspNet.Web.Optimization.它默认包含在 MVC4 RTM 应用程序中,无论您将目标框架指定为 4.5 还是 4.0.这是包含捆绑类的命名空间,并且似乎不依赖于框架.我已经部署到一个没有安装 4.5 的服务器,它仍然按我的预期工作.只需确保 DLL 与应用的其余部分一起部署.

UPDATE: I've found that what you need to have is the Nuget package Microsoft.AspNet.Web.Optimization installed in your project. It's included by default in an MVC4 RTM app regardless if you specify the target framework as 4.5 or 4.0. This is the namespace that the bundling classes are included in, and doesn't appear to be dependent on the framework. I've deployed to a server that does not have 4.5 installed and it still works as expected for me. Just make sure the DLL gets deployed with the rest of your app.

这篇关于为什么我的 CSS 捆绑不适用于 bin 部署的 MVC4 应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆