tau
tau

Reputation: 6749

get directory listing for bundling

I have a directory of files that I would like to be individually minified. However, BundleConfig.cs does not seem to let me use Server.MapPath, so I'm unsure of how I can write a loop to iterate over the individual files in a directory and bundle/minify them individually.

I'd like to do something like this in BundleConfig.cs:

string[] dir = Directory.GetFiles(Server.MapPath("~/stuff/css"));
foreach (....) {
    bundles.Add();
}

Upvotes: 1

Views: 671

Answers (1)

sga101
sga101

Reputation: 1904

From the documentation, there is a method IncludeDirectory which should do what you want.

bundles.Add(new StyleBundle("~/jQueryUI/themes/baseAll")
.IncludeDirectory("~/Content/themes/base", "*.css"));

There's a good tutorial on bundling on the asp.net site:

http://www.asp.net/mvc/tutorials/mvc-4/bundling-and-minification

To iterate them individually, this should work:

string virtualDirectory = "~/Styles";
string directory = HttpContext.Current.Server.MapPath(virtualDirectory);
foreach (string fileName in Directory.GetFiles(directory))
{
    bundles.Add(new StyleBundle("~style/minified/" + fileName).Include(virtualDirectory + "/" + fileName));
}

You will need to add a using for System.IO.

Upvotes: 2

Related Questions