javascript MVC 捆绑不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26685606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 06:18:48  来源:igfitidea点击:

MVC Bundling Not Working

javascriptcssasp.net-mvc-4model-view-controllerbundle

提问by kayze

I am working on a small mvc project, i have declared a bundle in BundleConfig.cs like this

我正在做一个小的 mvc 项目,我已经在 BundleConfig.cs 中声明了一个像这样的包

//javascript bundle

//javascript包

bundles.Add(new ScriptBundle("~/bundles/Layout1JS").Include(
                        "~/Content/Public/js/jquery.min.js",
                        "~/Content/Public/js/bootstrap.min.js",
                        "~/Content/Public/js/jquery.isotope.min.js",
                        "~/Content/Public/js/jquery.Photo.js",
                        "~/Content/Public/js/easing.js",
                        "~/Content/Public/js/jquery.lazyload.js",
                        "~/Content/Public/js/jquery.ui.totop.js",
                        "~/Content/Public/js/nav.js",
                        "~/Content/Public/js/sender.js",
                        "~/Content/Public/js/jquery.slider-min.js",
                        "~/Content/Public/js/custom.js"));


//css bundle
bundles.Add(new StyleBundle("~/Content/Public/css").Include(
                        "~/Content/Public/css/main.css"));

In my _Layout.cshml in the head section i have entered:

在 head 部分的 _Layout.cshml 中,我输入了:

<head>
    <meta charset="utf-8">
    <title>test</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">

    <!-- LOAD CSS FILES -->
    @Styles.Render("~/Content/Public/css", "~/Content/css")

    <!-- LOAD JS FILES -->


    @Scripts.Render("~/bundles/Layout1JS")

</head>

In my application_start i have:

在我的 application_start 我有:

BundleTable.EnableOptimizations = true; 

and web.config

和 web.config

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

This gave me bit of a headache trying to figure why the bundling isnt working specially for the javascript. Some one please advise

这让我有点头疼,试图弄清楚为什么捆绑不是专门为 javascript 工作的。有人请指教

回答by Tommy

The bundling is not working because of this line

由于这条线,捆绑不起作用

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

When in debug, the bundles do not compress or minify as you are "debugging" and being able to see the actual JavaScript and CSS is a good thing when you are debugging. When you have debug set to false (or removed from the tag), then your application is running in release mode. Bundling will occur at that point (unless you set BundleTable.EnableOptimizations = false;)

在调试时,包不会在您“调试”时压缩或缩小,并且在调试时能够看到实际的 JavaScript 和 CSS 是一件好事。当您将 debug 设置为 false(或从标记中删除)时,您的应用程序将在发布模式下运行。捆绑将在那时发生(除非您设置BundleTable.EnableOptimizations = false;

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

or

或者

<compilation targetFramework="4.0">

As pointed out by Mike below, the BundleTable.EnableOptimizations = true;should override the web.config setting. However, it may still be good to get out of debug mode in the case that your project is not overriding that setting.

正如下面 Mike 所指出的,BundleTable.EnableOptimizations = true;应该覆盖 web.config 设置。但是,在您的项目没有覆盖该设置的情况下,退出调试模式可能仍然很好。