How to bundle resources for ASP.NET MVC areas?

Martijn B picture Martijn B · Nov 11, 2012 · Viewed 21.7k times · Source

How would you do the resource bundling for asp.net mvc areas? Is this regulated by the ASP.NET MVC framework just like the AreaRegistration for routes?

I could make a BundleConfig class inside the area and call this from the global BundleConfig inside the App_Start folder but this doens't feel good to me.

I can't find any information on this subject. Any help our thoughts are appreciated.

Answer

Martijn B picture Martijn B · Nov 12, 2012

I was hoping this was somehow more regulated - but after diving into the framework code the answer to this is negative.

What I decided to do is the following:

Solution Structure

  • Areas:
    • Admin
      • RouteConfig.cs
      • BundleConfig.cs
      • AdminAreaRegistration.cs

RouteConfig.cs

internal static class RouteConfig
{
    internal static void RegisterRoutes(AreaRegistrationContext context)
    {
        //add routes
    }
}

BundleConfig.cs

internal static class BundleConfig
{
    internal static void RegisterBundles(BundleCollection bundles)
    {           
        //add bundles
    }
}

AdminAreaRegistration.cs

public class AdminAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get
        {
            return "Admin";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        RegisterRoutes(context);
        RegisterBundles();
    }

    private void RegisterRoutes(AreaRegistrationContext context)
    {
        RouteConfig.RegisterRoutes(context);
    }

    private void RegisterBundles()
    {
        BundleConfig.RegisterBundles(BundleTable.Bundles);            
    }       
}