A great new feature in ASP.NET MVC is the ability to bundle scripts and CSS files and register them as a group. More detail on this functionality can be found here. Because by default all the scripts and CSS classes in a new MVC 4 project are registered using bundles I felt a little dirty in my previous posts referencing my Kendo UI scripts and CSS files each individually. I rectified that.
So I started the rectification process by adding entries to the BundleConfig.cs. In this file is where you create and configure your bundles. Out of the box there are a number of bundles configured so all I needed to was follow the same conventions when creating my bundles.
Obviously I took the least amount of scripts I needed to get my contrived sample created, but you can add whatever scripts you need.
After creating the bundles I need to register or “Render” them as the method is called. Have to admit I am not a fan of “Render” as the method name. In the past “Render” has always had a visual connotation. The registering of the bundles for this example is taking place in the _layout.cs, but you could do the same thing directly in your ASP.NET MVC View.
@Styles.Render([...other CSS bundles], "~/Content/kendocss")
@Scripts.Render([...other Script bundles],"~/bundles/kendojs")
And that’s it. Now I have my Kendo UI scripts and CSS files registered/referenced/rendered using the new bundling features of ASP.NET MVC
Code is available on GitHub.
Keith is a Senior Software Engineer with Falafel Software. He has been developing software since 1999 specializing in web-based solutions primarily using the Microsoft stack. He has been a Microsoft MVP in ASP.NET since 2012.
The opinions expressed herein are my own personal opinions and do not represent
my employer's view in any way.