1

i am using BundleConfig to bundle my css and javascript files in mvc 4.0 project. just started using it, but somehow my bundled css file get 404 status from the server. wonder what is the problem.

Here is my setting;

public class BundleConfig
{
    public static void RegisterBundles(BundleCollection bundles)
    {
        bundles.Add(new ScriptBundle("~/CSSJSBundles/3rdpartycss").Include(
                    "~/Scripts/jquery.jnotify.css"

)); }

    BundleTable.EnableOptimizations = true;
}

I have created that folder: 'CSSJSBundles' in my root. Do i need to? or it is just virtual folder mvc uses? Also do i need to put any settings in Global.aspx?

I also deleted the folder, still there is 404 error for that bundled css file.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
HotFrost
  • 1,595
  • 4
  • 17
  • 25
  • Please show how you are referencing your bundle and also how the reference renders to the client. – MikeSmithDev Jan 28 '13 at 04:32
  • Also consider moving your css to a folder called "content". "Scripts" is typically for javascript files. – MikeSmithDev Jan 28 '13 at 04:48
  • Thanks for the answers all. First, great find on that 'StyleBundle'. I replaced it. Second, it still does not work. Do I have to have such a folder in my project 'CSSJSBundle'? Third, here is how I reference it in my _layout file: @Styles.Render("~/CSSJSBundles/3rdpartycss", "~/CSSJSBundles/widgetscss", "~/CSSJSBundles/controllerscss"); I tried to switch the debug mode to 'off' still does not work.. But isn't that line:'BundleTable.EnableOptimizations = true;' should take care about enabling bundling? Thanks for the help! – HotFrost Jan 28 '13 at 15:26
  • What happens when you just use `@Styles.Render("~/CSSJSBundles/3rdpartycss")` – MikeSmithDev Jan 28 '13 at 15:42
  • nothing.. file not found: Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost:13979/CSSJSBundles/3rdpartycss with or without actual folder in my solution 'CSSJSBundles' i am having the same error.. :( – HotFrost Jan 28 '13 at 16:29
  • try changing from debug=false in web.config – Dave Alperovich Jan 28 '13 at 16:57
  • ok.. I changed the debug = false, I looked into the source.. and it seems bundling does not work, as the links stay in the same form as in my original code: @Styles.Render("~/bundles/3rdpartycss", "~/bundles/widgetscss", "~/bundles/controllerscss") As far as I understand there is supposed to be a hash info added for versioning.. Also, reading the blog posts, I see that many refer to the entry in my global.asax.cs page - 'EnableDefaultBundles()'. BUT!, there is just no such method in BundleTable.Bundles class anymore!.. Any ideas? – HotFrost Jan 28 '13 at 17:06
  • @DaveA his `EnableOptimizations=true` forces it to bundle even in debug mode. – MikeSmithDev Jan 28 '13 at 17:46
  • 1
    @user194033 your global.asax.cs should have `BundleConfig.RegisterBundles(BundleTable.Bundles);` if you are using MVC4. – MikeSmithDev Jan 28 '13 at 17:47
  • yes, adding 'BundleConfig.RegisterBundles(BundleTable.Bundles);' to global.asax.cs fixed that! Thanks Mike! – HotFrost Jan 28 '13 at 17:53

2 Answers2

6
  1. As noted in one of the previous answers, you need to change ScriptBundle to StyleBundle.

  2. BundleTable.EnableOptimizations = true; isn't required. What that does is force bundling and minification to occur when you are in debug mode. The default action is not NOT bundle/minify in debug mode so you can debug more easily. Use that line when you want to see the output as it will happen in release mode.

  3. No you do not need that folder CSSJSBundles to physically exist.

  4. It appears you need to add this line to your Application_Start() in your global.asax.cs file: BundleConfig.RegisterBundles(BundleTable.Bundles);

MikeSmithDev
  • 15,731
  • 4
  • 58
  • 89
5

u have to use StyleBundle ... not a ScriptBundle since it is a css file.

for js files, u should use ScriptBundle.

try

public static void RegisterBundles(BundleCollection bundles)
        {
            bundles.Add(new StyleBundle("~/bundles/3rdpartycss").Include(
                        "~/Content/jquery.jnotify.css"
                        ));
        }

:)

Bilal Fazlani
  • 6,727
  • 9
  • 44
  • 90