0

I am very new to Bundling and minification, I try to implement it for the first time in my MVC project.

I have added a BundleConfig.cs file:

public class BundleConfig { public static void RegisterBundles(BundleCollection bundles) { //libs scripts bundles.Add(new ScriptBundle("~/bundles/jquery").Include( "~/Scripts/libs/jquery/jquery-{version}.js", "~/Scripts/libs/jquery/jquery-ui-{version}.js", "~/Scripts/libs/jquery/jquery.mask*", "~/Scripts/libs/jquery/jquery.validate*")); bundles.Add(new ScriptBundle("~/bundles/ko").Include( "~/Scripts/libs/ko/knockout-{version}.js")); //site scripts bundles.Add(new ScriptBundle("~/bundles/site").Include( "~/Scripts/site/*.js")); bundles.Add(new StyleBundle("~/Content/site/").Include("~/Content/site/*.css")); } } 

And added in Global.asax:

BundleConfig.RegisterBundles(BundleTable.Bundles); 

Then I rendered the scripts/css in my Layout page:

<head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>@ViewBag.Title</title> @Styles.Render("~/Content/site/Fonts.css") @Styles.Render("~/Content/site/Site.css") @RenderSection("styles", required: false) @Scripts.Render("~/bundles/jquery") @Scripts.Render("~/bundles/ko") @Scripts.Render("~/bundles/site") @RenderSection("scripts", required: false) </head> 

But this is not working, I keep getting all kind of errors that indicate that the scripts and css are not being recognized.

For example:

Uncaught ReferenceError: jQuery is not defined

What am I doing wrong?

5
  • Don't add the files manually and using the bundles - delete all your <script src="... code. Commented Mar 23, 2017 at 9:55
  • @StephenMuecke Thanks, I don't have both, I copied it like this by mistake... Commented Mar 23, 2017 at 9:56
  • Are you duplicating any of those in the main view? Commented Mar 23, 2017 at 9:58
  • @StephenMuecke no, I double check. Commented Mar 23, 2017 at 10:00
  • Have you checked the sources tab in the browser tools to see if they are loaded. Commented Mar 23, 2017 at 10:08

2 Answers 2

1

As per @BasantaMatia comment that gave me the idea, setting:

BundleTable.EnableOptimizations = true 

In the Global.asax file, after:

BundleConfig.RegisterBundles(BundleTable.Bundles); 

Solved the issue.

Sign up to request clarification or add additional context in comments.

Comments

0

See here you are adding 2 times all the scripts files,

You already added reference your bundle jquery, ko and site.

@Scripts.Render("~/bundles/jquery") @Scripts.Render("~/bundles/ko") @Scripts.Render("~/bundles/site") 

Then there is no need to add again these files.

<script src="~/Scripts/Libs/jquery/jquery-3.1.1.min.js"></script> <script src="~/scripts/libs/jquery/jquery-ui-1.12.1.min.js"></script> <script src="~/Scripts/Libs/jquery-mask/jquery.mask.min.js"></script> <script src="~/Scripts/Libs/knockout/knockout-3.4.1.js"></script> <script src="~/Scripts/Site/Site.js"></script> 

2 Comments

See my comment above.
Can you add this line "BundleTable.EnableOptimizations = false;" after RegisterBundles(BundleCollection bundles) method in BundleConfig file and check

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.