So I was able to get this working by adding a custom VirtualPathProvider which makes the main project search within the static content project for the individual files. When in DEBUG mode, the files are listed individually. When in RELEASE mode, the minified bundles are referenced instead.
public class BundleConfig
{
public static void RegisterBundles(BundleCollection bundles)
{
var staticWebsite = ConfigurationManager.AppSettings["StaticWebsite"];
var versionNumber = ConfigurationManager.AppSettings["VersionNumber"];
Styles.DefaultTagFormat = string.Format("<link href='{0}{{0}}?v={1}' rel='stylesheet'/>", staticWebsite, versionNumber);
Scripts.DefaultTagFormat = string.Format("<script src='{0}{{0}}?v={1}'></script>", staticWebsite, versionNumber);
#if DEBUG
// Includes files from the static content project so they can be listed individually if in DEBUG mode.
BundleTable.VirtualPathProvider = new StaticContentVirtualPathProvider();
bundles.Add(new StyleBundle("~/bundles/styles").IncludeDirectory("~/app/styles", "*.css", true));
bundles.Add(new ScriptBundle("~/bundles/scripts").IncludeDirectory("~/app/src", "*.js", true));
#endif
}
}
Here is my custom VirtualPathProvider:
public class StaticContentVirtualPathProvider : VirtualPathProvider
{
// Modify this to be the relative path from your main project to your static content project.
private const string StaticContentRelativePath = @"..\..\MyStaticContentProjectFolder";
public static string GetStaticContentPath(string virtualPath, bool isDirectory = false)
{
virtualPath = virtualPath.Replace('/', '\\').Replace("~", "");
if (isDirectory && !virtualPath.EndsWith("\\")) virtualPath += "\\";
return HttpRuntime.AppDomainAppPath + StaticContentRelativePath + virtualPath;
}
public override bool FileExists(string virtualPath)
{
return File.Exists(GetStaticContentPath(virtualPath));
}
public override bool DirectoryExists(string virtualDir)
{
return Directory.Exists(GetStaticContentPath(virtualDir));
}
public override VirtualFile GetFile(string virtualPath)
{
return new StaticContentVirtualFile(virtualPath);
}
public override VirtualDirectory GetDirectory(string virtualDir)
{
return new StaticContentVirtualDirectory(virtualDir);
}
private class StaticContentVirtualFile : VirtualFile
{
public StaticContentVirtualFile(string virtualPath)
: base(virtualPath)
{
this.virtualPath = virtualPath;
}
private readonly string virtualPath;
public override Stream Open()
{
return new FileStream(StaticContentVirtualPathProvider.GetStaticContentPath(virtualPath), FileMode.Open);
}
}
internal class StaticContentVirtualDirectory : VirtualDirectory
{
public StaticContentVirtualDirectory(string virtualPath)
: base(virtualPath)
{
}
public override IEnumerable Files
{
get
{
var filePaths = Directory.GetFiles(GetStaticContentPath(this.VirtualPath, true));
var files = filePaths.Select(filePath => new StaticContentVirtualFile(this.VirtualPath + Path.GetFileName(filePath))).ToList();
return files;
}
}
public override IEnumerable Directories
{
get
{
var subDirectoryPaths = Directory.GetDirectories(GetStaticContentPath(this.VirtualPath, true));
var dirs = subDirectoryPaths.Select(subDirectoryPath => new StaticContentVirtualDirectory(this.VirtualPath + Path.GetFileName(subDirectoryPath))).ToList();
return dirs;
}
}
public override IEnumerable Children { get { throw new NotImplementedException(); } }
}
}