2

I read Scott Gu's article about built-in support for bundling and minification in ASP .NET 4.5.

However there's no mention of embedded resources, which is a pity.

In the past I've been using a Codeplex project called Client Dependency Framework which supported embedded resources.

Seems like a pretty major omission to me. Is support planned?

cbp
  • 25,252
  • 29
  • 125
  • 205

3 Answers3

3

I'm pretty sure you could write your own transformer to handle this.

  1. Create a class that implements System.Web.Optimization.IBundleTransform.
  2. Then in the Process method get the contents of the embedded resource. This shouldn't be too difficult. This blog post might be helpful.
  3. Then add the transform to the bundle.

e.g.

var bundle = new Bundle("~/Test").Include("~/Content/Site.css");
bundle.Transforms.Add(new EmbeddedResourceTransformer());

Note that I am using the nuget package from System.Web.Optimization, not Microsoft.Web.Optimization (I have no idea why there are two different namespace implementations, and whether the syntax would be the same in both).

I also can't vouch for the performance of doing it this way as opposed to the file system.

Hope that helps!

Chris Haines
  • 6,445
  • 5
  • 49
  • 62
2

Just a few comments on above answers since I don't have enough rep. to comment directly...

The answer from Hainesy suggests using a BundleTransform. I believe this is too late in the process to include an embedded resource. The BundleTransform is helpful for converting things inside the css or javascript after the contents are pulled from the original file and before they are put into the bundled file. For example, if you need to modify image URL's in CSS to point to local relative url for dev and to a CDN url for production.

The link from user960567 explains how to use embedded resources, but there's a catch. That process will only work for something like a common control used from another project.
e.g. If you create a textbox that needs CSS and JS then it allows you to create a HTML helper in the common project that will add the textbox and the script tags that pull in the embedded resource into the page. It does not allow you to pull the embedded resource from the common project into a bundle in another project. This will basically create a separate script or style tag for each embedded resource which may not be what you want (at least it's not what I was looking for.)

JeffR
  • 1,782
  • 1
  • 15
  • 19
-3

I've written a detailed article about how you can use the bundle and minification technology to wrap up your external resources here.

Community
  • 1
  • 1
Tim
  • 298
  • 1
  • 2
  • 11