MVC Bundle and Minification at Sitecore
2014/11/012 min read
bookmark this
Table of Contents
- Introduction
- Web.config
- Register Bundle to Sitecore - Part 1
- Register Bundle to Sitecore - Part 2
- Conclusion
- References
Introduction
This blog shows how to enable ASP.NET Bundle & Minification for JavaScript and CSS in Sitecore. The following example is based on these technologies:
-
Sitecore 7.2
-
ASP.NET MVC 5
-
Visual Studio 2013
Web.config
Add the following module to the web.config modules section:
Register Bundle to Sitecore - Part 1
For registering the bundle, you need to add the following code.
Note that you don't need to add anything in the Global.asax class to register bundles, as you would with default ASP.NET MVC bundle behavior.
using Sitecore;
using Sitecore.Pipelines;
using System.Web.Optimization;
namespace YourNamespace
{
///
/// SiteCore bundle register
///
public class RegisterBundles
{
[UsedImplicitly]
public virtual void Process(PipelineArgs args)
{
Bundles(BundleTable.Bundles);
}
///
/// add bundle collection when application start
/// at here, you can add as regular
///
///
private void Bundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js"));
}
}
}
Register Bundle to Sitecore - Part 2
You need to add the following config file. Any name is fine — Sitecore will pick it up. Let's call it bundle.config. Put the file under {YourAspnetWebSiteFolder}/App_Config/Include/{PutHere}, or any location where your site loads the Sitecore config.
```
That's it! You should be able to use ASP.NET Bundle & Minification now as follows:
@Scripts.Render("~/bundles/jquery")
## Conclusion
Enabling ASP.NET Bundle & Minification in Sitecore requires adding the bundle module to the web.config, creating a bundle registration class, and adding a Sitecore pipeline config file. Once set up, you can use bundles just like in a standard ASP.NET MVC application.
## References
- Sitecore with MVC4