5

I have been using Bundles in MVC to pack all script and CSS together which is great but.... Is there any way to include script or css from resources in a shared project library in a Bundle, or does anyone know of something similar to bundles that can do this?

1
  • 2
    Hi, there. Did you get a solution for this ? Commented Jul 26, 2012 at 6:15

1 Answer 1

1

I would probably start out writing a custom bundle transform class to read the resources you need and return their content and content type:

public class ResourceTransform : IBundleTransform
{
    public void Process(BundleContext context, BundleResponse response)
    {
        string result;

        using (Stream stream = Assembly.GetExecutingAssembly()
            .GetManifestResourceStream("YourAssemblyNamespace.YourResourceFolder.YourFile.css"))
        {
            using (StreamReader reader = new StreamReader(stream))
            {
                result = reader.ReadToEnd();
            }
        }

        response.ContentType = "text/css";
        response.Content = result;
    }
}

For production use you probably want to make the ResourceTransform class a little bit less hardcoded and send the resources you want as params or properties, but you get the idea.

That way you can add this bundle to your collection:

Bundle resources = new Bundle("~/css/resources");
    resources.Transforms.Add(new ResourceTransform());
    resources.Transforms.Add(new CssMinify());

bundles.Add(resources);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.