当我打开释放模式时,捆绑在MVC5中不工作 [英] Bundling not working in MVC5 when I turn on release mode

查看:336
本文介绍了当我打开释放模式时,捆绑在MVC5中不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在BundleConfig.cs中配置了以下包:

  bundles.Add(new StyleBundle(〜/ bundles / css)。Include(
〜/ assets / bootstrap / css / bootstrap.css,
〜/ assets / css / global / all.css));

,并使用以下代码引用它:

  @ Styles.Render(〜/ bundles / css)

当我在调试模式(web.config编译 debug =true)它的工作原理是它渲染两个css文件正常ie: / p>

 < link href =/ assets / bootstrap / css / bootstrap.css =stylesheet/& 
< link href =/ assets / css / global / all.css =stylesheet/>

但是当我设置 debug =false上述行为仍会发生,因为它确实识别文件,但它只是将它们正常渲染。



确认捆绑可以明确地工作我已经启用优化BundleConfig即 BundleTable.EnableOptimizations = true;



每当我做上述操作,它捆绑css, :

 < link href =/ bundles / css?v = WBKHkZAJly7jUzHrVDT8SwfaQE-CA9dbOUQUlLKadNE1 =stylesheet/& 

编辑:



将以下代码添加到我的BundleConfig.cs文件将实现我的后:

  #if DEBUG 
BundleTable.EnableOptimizations = false;
#else
BundleTable.EnableOptimizations = true;
#endif

我理解并感谢此响应,但根据文档, MVC绑定的行为是在发布模式下捆绑,但不在调试模式下捆绑。我不知道为什么我应该需要添加额外的代码,以使它这样做,当它应该这样做。



编辑2



我有一个供认。原来我有从打开的Views文件夹的web.config,而不是主web.config。我改变了主web.config中的设置,这对我来说很好。我责怪ReSharper

解决方案

这是默认行为。


通过在Web.config文件中的编译元素中设置debug属性的值来启用或禁用绑定和缩减。


http://www.asp.net/mvc / overview / performance / bundling-and-minification


I have the following bundle configured in BundleConfig.cs:

bundles.Add(new StyleBundle("~/bundles/css").Include(
                      "~/assets/bootstrap/css/bootstrap.css",
                      "~/assets/css/global/all.css"));

and I reference it using the following:

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

When I'm in debug mode (web.config compilation debug="true") it works as expected in that it renders both css files as normal ie:

<link href="/assets/bootstrap/css/bootstrap.css" rel="stylesheet"/>
<link href="/assets/css/global/all.css" rel="stylesheet"/>

However when I set debug="false" the above behaviour still occurs in that it does recognise the files, however it's just rendering them as normal.

To confirm bundling can definitely work I've enabled optimizations in BundleConfig ie BundleTable.EnableOptimizations = true;

Whenever I do the above, it bundles the css and appears as expected ie:

<link href="/bundles/css?v=WBKHkZAJly7jUzHrVDT8SwfaQE-CA9dbOUQUlLKadNE1" rel="stylesheet"/>

EDIT:

A few people have mentioned that adding the following code to my BundleConfig.cs file will achieve what I am after:

#if DEBUG
            BundleTable.EnableOptimizations = false;
#else
            BundleTable.EnableOptimizations = true;
#endif

I understand and appreciate this response, however according to the documentation, the default behaviour of MVC bundling is to bundle in release mode but not in debug mode. I don't see why I should need to add extra code to make it do this when it should be doing it already.

EDIT 2

I've a confession to make. It turns out I had the web.config from the Views folder opened and not the main web.config. I changed the setting in the main web.config and this works just fine for me. I blame ReSharper

解决方案

This is the default behavior.

Bundling and minification is enabled or disabled by setting the value of the debug attribute in the compilation Element in the Web.config file.

http://www.asp.net/mvc/overview/performance/bundling-and-minification

这篇关于当我打开释放模式时,捆绑在MVC5中不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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