4

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?

StanK
  • 4,750
  • 2
  • 22
  • 46

2 Answers2

6

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.

Ivaylo Stoev
  • 457
  • 3
  • 7
  • 3
    great thanks. For what it's worth, I used `BundleTable.Bundles.Any(y => y.Path == bundleName)` – StanK Jan 22 '17 at 22:43
0

I'm not aware of any way (nor was I able to find a way) for you to do this that is built into the framework. If you really have to do this, I would point you to a solution by Herman.

Asp.Net MVC Bundling, best way to detect missing file

Are your bundles dynamic? If not, I would suggest that this may not be something you need. Once you have them setup correctly the first time, they shouldn't fail.

Community
  • 1
  • 1
Brendan Long
  • 276
  • 1
  • 6