How to Change Elements of an MVC Collection Collection Dynamically

I want if I could change the registered package collection elements (JS or CSS files) dynamically after publishing my MVC website by clicking a button. Is this possible and what should we do to achieve this?

any help is appreciated.

+4
source share
1 answer

After some searching and trying, I found a way. I put this snippet in Session_Start, but you can use it anywhere, for example, in a button dispatch handler. I search for a Bundle with a special name, then delete and create a new package with the same name that I used in my project. ( I did this to have a new fresh package, because I cannot find any method to delete files from an existing package, and I appreciate if anyone knows how to do this ). Finally, add the files to the package with the bundle method enabled and add the bundle to the BundleTable. (You can also use the IncludeDirectory method)

Bundle bndl = BundleTable.Bundles.GetBundleFor("~/Content/css");
            if (bndl != null)
            {
                BundleTable.Bundles.Remove(bndl);
            }

            Bundle bndl2 = new Bundle("~/Content/css");
            bndl2.Include("~/Content/site.css", "~/Content/secondStyles.css", ... );
            BundleTable.Bundles.Add(bndl2);

Recently, it has been helpful to read this article for working with packages. Asp.net MVC 4 performance optimization with snapping and minimizing

. .

+3

All Articles