StanK
StanK

Reputation: 4770

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?

Upvotes: 5

Views: 2006

Answers (2)

Ivaylo Stoev
Ivaylo Stoev

Reputation: 487

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.

Upvotes: 7

Brendan Long
Brendan Long

Reputation: 286

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.

Upvotes: 1

Related Questions