Custom bundle in MVC 4 is not rendered

I am working on ASP.NET MVC 4 application. I want to make my own bundle containing .js files which I will use for validation but for some reason it's not working. Just to mention - if I render all my scripts in a given view everything is working fine like:

@Scripts.Render("~/Scripts/jquery-1.7.1.min.js")
@Scriptes.Render("~/Scripts/jquery.validate.min.js")
//..few more files

but I don't want to include all those scripts each time so I decided to make my own bundle. The steps I made are:

  • In BundleConfig.cs I have this :
  • public static void RegisterBundles(BundleCollection bundles)

    {

    //The defaults bundles

    bundles.Add(new ScriptBundle("~/bundles/customval").Include(

        "~/Scripts/jquery-1.7.1.min.js",
        "~/Scripts/jquery.validate.min.js",
        "~/Scripts/jquery.unobtrusive-ajax.min.js",
        "~/Scripts/jquery.validate.unobtrusive.min.js"));
    

    }

    Then I check my Global.asax where I have this :

    BundleConfig.RegisterBundles(BundleTable.Bundles);

    and then in my view I just try :

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

    and FireBug is saying that no JavaScript is load for this page.

    I use custom _Layout page and I wonder if I have to add something there. I added:

    @RenderSection("scripts", required: false)
    

    in my _Layout but it doesn't seem to solve something. So what am I missing to make my bundles working?


    You're probably running in DEBUG mode. in this mode .min.js files are ignored (as they're only intended for Production use).

    It would be best to include the non-minified (sometimes suffixes with .debug.js ) versions, then on RELEASE mode the Optimization Framework will Bundle and Minify it for you automatically (unless you specify otherwise).


    I have had to add:

    BundleTable.EnableOptimizations = false;
    

    at the end of the RegisterBundles method. In my old age :) I can't remember exactly what the issue was but it has to do with the .min files and debugging if memory serves me right.

    The @RenderSection("scripts", required: false) is to allow you to have a special scripts section (optional in your case) defined in each view and have it rendered in the same place in every page that uses the _layout file.

    链接地址: http://www.djcxy.com/p/67980.html

    上一篇: 在MVC中创建包的虚拟路径?

    下一篇: 未呈现MVC 4中的自定义包