Skip to content

Instantly share code, notes, and snippets.

@xpando
Created February 20, 2012 00:07
Show Gist options
  • Save xpando/1866680 to your computer and use it in GitHub Desktop.
Save xpando/1866680 to your computer and use it in GitHub Desktop.
Use config files with ASP.NET 4.5 Bundles
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width" />
<title>@ViewBag.Title</title>
<link href="@BundleTable.Bundles.ResolveBundleUrl("~/content/css")" rel="stylesheet" type="text/css" />
</head>
<body>
<header>
<div class="content-wrapper">
<div class="float-left">
<p class="site-title"><a href="/">ASP.NET Web API</a></p>
</div>
</div>
</header>
@RenderBody()
<script src="@BundleTable.Bundles.ResolveBundleUrl("~/scripts/common")" type="text/javascript"></script>
</body>
</html>
jquery-1.7.1.js
jquery-ui-1.8.17.js
jquery.unobtrusive-ajax.js
jquery.validate.js
knockout-2.0.0.debug.js
modernizr-2.0.6-development-only.js
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Optimization;
using System.IO;
namespace MvcApplication3
{
public static class ExtendBundleCollection
{
public static void ConfigureJsBundles(this BundleCollection bundles, string virtualPath)
{
virtualPath = VirtualPathUtility.AppendTrailingSlash(virtualPath);
var path = HttpContext.Current.Server.MapPath(virtualPath);
if (Directory.Exists(path))
{
foreach (var config in Directory.GetFiles(path, "*.bundle"))
{
var bundleName = Path.GetFileNameWithoutExtension(config);
var bundlePath = VirtualPathUtility.Combine(virtualPath, bundleName);
var bundle = new Bundle(bundlePath, new JsMinify());
var scriptPaths = File.ReadLines(config);
foreach (var relPath in scriptPaths)
{
var scriptPath = VirtualPathUtility.Combine(virtualPath, relPath);
bundle.AddFile(scriptPath, true);
}
bundles.Add(bundle);
}
}
}
public static void ConfigureCssBundles(this BundleCollection bundles, string virtualPath)
{
virtualPath = VirtualPathUtility.AppendTrailingSlash(virtualPath);
var path = HttpContext.Current.Server.MapPath(virtualPath);
if (Directory.Exists(path))
{
foreach (var config in Directory.GetFiles(path, "*.bundle"))
{
var bundleName = Path.GetFileNameWithoutExtension(config);
var bundlePath = VirtualPathUtility.Combine(virtualPath, bundleName);
var bundle = new Bundle(bundlePath, new CssMinify());
var scriptPaths = File.ReadLines(config);
foreach (var relPath in scriptPaths)
{
var scriptPath = VirtualPathUtility.Combine(virtualPath, relPath);
bundle.AddFile(scriptPath, true);
}
bundles.Add(bundle);
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
namespace MvcApplication3
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
public class WebApiApplication : System.Web.HttpApplication
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
//BundleTable.Bundles.RegisterTemplateBundles();
BundleTable.Bundles.ConfigureCssBundles("~/content");
BundleTable.Bundles.ConfigureJsBundles("~/scripts");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment