Search code examples
asp.net-mvcasp.net-mvc-5

How to check if MVC bundle exists


Is there a way to test if a bundle exists before trying to render it?

I want to do something like this:

@{
    var bundleName = GetBundleName();
}

@if (Scripts.BundleExists(bundleName))
{
    @Scripts.Render(bundleName)
}

Obviously, Scripts.BundleExists() isn't real, but it there something build in that does this? Or do I have to implement this myself?


Solution

  • You can get the bundles in the View by:

    var bl = System.Web.Optimization.BundleTable.Bundles;

    Then you can search the collection for a specific bundle by path as registered in BundleConfig. After that check if the path or any of the included paths exist.