Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can the method "Bundle.Include" (in System.Web.Optimization used with ASP.NET MVC) throw an exception instead of silently ignore missing files?

Is it somehow possible to get an exception thrown if a javascript or css file is missing ?

I have tried with the code below but it never throws an exception ...

    public class BundleConfig {
    public static void RegisterBundles(BundleCollection bundles) {  
        ....

        bundles.Add(new ScriptBundle("~/bundles/something").Include("~/Scripts/nonExistingFile.js"));
        // I would like the above usage of Include method with 
        // a non-existing file  to throw an exception to make it 
        // obvious that an expected file is missing ...
        // but since it does not I tried to implement a method as below ...
        ...
        AssertThatAllFilesExist(bundles);
        // but unfortunately an exception is never thrown but when 
        // iterating the files in the method below, 
        // the non-existing file seems to never become retrieved ...
    }   

    private static void AssertThatAllFilesExist(BundleCollection bundles) {
        HttpContext currentHttpContext = HttpContext.Current;
        var httpContext = new HttpContextWrapper(currentHttpContext);
        foreach (var bundle in bundles) {
            var bundleContext = new BundleContext(httpContext, bundles, bundle.Path);
            IEnumerable<FileInfo> files = bundle.EnumerateFiles(bundleContext);
            foreach (var file in files) {
                if (!file.Exists) {
                    // if this problem would occur then it should 
                    // indicate that one of the arguments of the 
                    // method "Bundle.Include" does not 
                    // correspond to an existing file ...
                    throw new ArgumentException("The following file does not exist: " + file.FullName);
                }
            }
        }
    }
}   
like image 739
user310457 Avatar asked Jan 25 '13 23:01

user310457


People also ask

Can we use bundling and minification with ASP NET web forms like MVC?

Bundling and minification are two techniques you can use in ASP.NET 4.5 to improve request load time. Bundling and minification improves load time by reducing the number of requests to the server and reducing the size of requested assets (such as CSS and JavaScript.)

What is use of bundle in ASP NET MVC?

Bundling is one of the features of MVC. By implementing this, we can improve performance request load time. Minification is the process of removing unnecessary data without changing its functionality such as removing white spaces, comments, converting the large variable names to small, etc.

What is the advantage of bundling in MVC?

Bundling and Minification provide us a way to both reduce the number of requests needed to get JS and CSS resource files and reduce the size of the files themselves, thereby improving the responsiveness of our apps. They are nice little optimizations for our MVC apps that can improve performance and add responsiveness.

What are the two types of bundles in MVC 5?

Bundle Types ScriptBundle: ScriptBundle is responsible for JavaScript minification of single or multiple script files. StyleBundle: StyleBundle is responsible for CSS minification of single or multiple style sheet files.


1 Answers

I use this class instead of ScriptBundle:

public class ScriptBundleWithException : ScriptBundle
{
    public ScriptBundleWithException( string virtualPath ) : base( virtualPath ) {}
    public ScriptBundleWithException( string virtualPath, string cdnPath ) : base( virtualPath, cdnPath ) {}

    public override Bundle Include( params string[] virtualPaths )
    {
        foreach ( var virtualPath in virtualPaths ) {
            Include( virtualPath );
        }
        return this;
    }

    public override Bundle Include( string virtualPath, params IItemTransform[] transforms )
    {
        var realPath = System.Web.Hosting.HostingEnvironment.MapPath( virtualPath );
        if ( !File.Exists( realPath ) ) {
            throw new FileNotFoundException( "Virtual path not found: " + virtualPath );
        }
        return base.Include( virtualPath, transforms );
    }
}

Then in configuration:

bundles.Add( new ScriptBundleWithException( "~/bundles/something" ) //...
like image 86
nrodic Avatar answered Sep 20 '22 00:09

nrodic