问题:为何 MVC4 bundler 不能包含 .min 文件

本文介绍了解决ASP.NET MVC项目中遇到的前端JS文件未能正确加载的问题,包括修改文件名、调整IgnoreList配置等方法,并探讨了不同环境下自动选择debug或minified版本的机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    项目中碰到了这个问题,问题如下:

bundles.Add(new ScriptBundle("~/scripts/package.js").Include(

       "~/scripts/jquery-paged-scroll.min.js",                

"~/scripts/liqu.dialog.js"));

调试状态下,前台页面源代码中完全找不到jquery-paged-scroll.min.js这个文件

google一下,有如下解决方案:

1  改名字,jquery-paged-scroll.min.js改名为jquery-paged-scroll.js

2  使用如下方法

 public static void AddDefaultIgnorePatterns(IgnoreList ignoreList)
    {
        if (ignoreList == null)
            throw new ArgumentNullException("ignoreList");
        ignoreList.Ignore("*.intellisense.js");
        ignoreList.Ignore("*-vsdoc.js");
        ignoreList.Ignore("*.debug.js", OptimizationMode.WhenEnabled);
        //ignoreList.Ignore("*.min.js", OptimizationMode.WhenDisabled);
        ignoreList.Ignore("*.min.css", OptimizationMode.WhenDisabled);
    }

    public static void RegisterBundles(BundleCollection bundles)
    {
        bundles.IgnoreList.Clear();
        AddDefaultIgnorePatterns(bundles.IgnoreList);

        //...your code
     }
更多解释

Microsoft implies the following behavior (and I prefer to follow it in my projects):

// short version
  1. 同一个项目文件夹下同时有debug和minified版本的script:
    • script.js
    • script.min.js
  2. 你在你的代码中bundle时只添加了 script.js

结果是:Debug模式下自动获得script.js版本,而release模式下自动获得script.min.js 

// long version

You can have also .debug.js version. In that case the file is included in the following priority in DEBUG:

  1. script.debug.js
  2. script.js

in RELEASE:

  1. script.min.js
  2. script.js

.// Note

另外,之所以在文件夹下放一个.min的版本的唯一原因是,在MVC4中有时minified版本不能被自动处理,如下代码将不能自动的进行minified处理:if (DEBUG) console.log("Debug message");

其他任何情况下,只需放一个debug版本
参考:
http://stackoverflow.com/questions/12569259/asp-net-mvc-4-scriptbundle-returns-empty
http://stackoverflow.com/questions/11980458/mvc4-bundler-not-including-min-files#comment15973197_11980458
http://stackoverflow.com/questions/11209396/why-is-mvc4-bundling-and-minification-making-my-files-bigger
http://timgthomas.com/2012/09/a-quick-start-of-asp-net-mvc-4s-bundling/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值