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 Burnell is a Microsoft web MVP and Senior Software Engineer with Skyline Technologies and president of the Fox Valley .Net User Group. Keith has been developing software for over 10 years specializing in large scale ASP.NET and ASP.NET MVC web site development and architecture
The opinions expressed herein are my own personal opinions and do not represent
my employer's view in any way.