MVC4 - 当优化设置为 true 时,捆绑不起作用 [英] MVC4 - Bundling does not work when optimizations are set to true

查看:27
本文介绍了MVC4 - 当优化设置为 true 时,捆绑不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我在这里做错了什么.我正在使用 ASP.NET C# MVC4,我想使用新的 css/js 优化功能.

I wonder what I don't do correct here. I am using ASP.NET C# MVC4 and I want to take use of new css/js optimization feature.

这是我的 HTML 部分

Here is my HTML part

@Styles.Render("~/content/css")

这是我的 BunduleConfig.cs 部分

bundles.Add(new StyleBundle("~/content/css").Include(
                        "~/content/css/reset.css",
                        "~/content/css/bla.css"));

// BundleTable.EnableOptimizations = true;

输出(作品):

<link href="/content/css/reset.css" rel="stylesheet"/>
<link href="/content/css/bla.css" rel="stylesheet"/>

但是当我取消注释 BundleTable.EnableOptimizations = true; html 输出看起来像这样

However when I uncomment BundleTable.EnableOptimizations = true; html output looks like this

<link href="/content/css?v=5LoJebKvQJIN-fKjKYCg_ccvmBC_LF91jBasIpwtUcY1" rel="stylesheet"/>

这当然是 404.我不知道我哪里做错了,请帮忙,第一次使用 MVC4.

And this is, of course is 404. I have no idea where I did something wrong, please help, first time working with MVC4.

推荐答案

我想问题是你把包放在一个实际存在的虚拟 URL 上,但它是一个目录.

I imagine the problem is you putting the bundle at a virtual URL that actually exists, but is a directory.

MVC 正在从您的包制作一个虚拟文件,并从您指定为包路径的路径提供它.

MVC is making a virtual file from your bundle and serving it up from the path you specify as the bundle path.

该问题的正确解决方案是使用不直接映射到现有目录的包路径,而是使用该目录内的虚拟文件名(也不会映射到真实文件名).

The correct solution for that problem is to use a bundle path that does not directly map to an existing directory, and instead uses a virtual file name (that also does not map to a real file name) inside that directory.

示例:

如果您的站点有一个名为/content/css 的文件夹,请按如下方式制作您的 css 包:

If your site has a folder named /content/css, make your css bundle as follows:

在 BundleConfig.cs 中:

In BundleConfig.cs:

bundles.Add(new StyleBundle("~/content/css/AllMyCss.css").Include(
                        "~/content/css/reset.css",
                        "~/content/css/bla.css"));

在页面上:

@Styles.Render("~/content/css/AllMyCss.css")

请注意,这里假设您的 css 文件夹中没有名为 AllMyCss.css 的文件.

Note that this assumes you do NOT have a file named AllMyCss.css in your css folder.

这篇关于MVC4 - 当优化设置为 true 时,捆绑不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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