首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >方法"Bundle.Include“(在与ASP.NET MVC一起使用的System.Web.Optimization中)可以抛出异常,而不是静默地忽略丢失的文件吗?

方法"Bundle.Include“(在与ASP.NET MVC一起使用的System.Web.Optimization中)可以抛出异常,而不是静默地忽略丢失的文件吗?
EN

Stack Overflow用户
提问于 2013-01-26 07:26:06
回答 5查看 2.4K关注 0票数 19

如果一个javascript或css文件丢失了,有没有可能抛出异常?

我已经尝试了下面的代码,但它从来没有抛出异常...

代码语言:javascript
复制
    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);
                }
            }
        }
    }
}   
EN

回答 5

Stack Overflow用户

发布于 2014-07-18 03:45:35

我使用这个类而不是ScriptBundle

代码语言:javascript
复制
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 );
    }
}

然后在配置中:

代码语言:javascript
复制
bundles.Add( new ScriptBundleWithException( "~/bundles/something" ) //...
票数 6
EN

Stack Overflow用户

发布于 2013-11-22 17:14:34

如果你查看Bundle类的源代码,你会发现如果文件不存在,.Include就不会默默地添加文件。因此,您的断言不起作用。

最后,我使用了一个脚本文件名的数组,并在添加到包中之前手动检查它们。

票数 2
EN

Stack Overflow用户

发布于 2014-02-18 10:01:32

我偶然发现了这一点,并认为我应该提交我的解决方案。正如其他人提到的,添加不存在于包中的文件会被默默地忽略。

示例代码:

代码语言:javascript
复制
string[] jsFiles = { "~/js/file1.js", "~/js/file2.js" };
string[] cssFiles = { "~/css/file1.css", "~/css/file2.css" };
List<string> allFiles = new List<string>();
allFiles.AddRange(jsFiles);
allFiles.AddRange(cssFiles);
var server = HttpContext.Current.Server;
foreach(var file in allFiles) {
    if(!File.Exists(server.MapPath(file))
        throw new FileNotFoundException("File " + file + " was not found.");
}

然后,使用用于.Include的数组,像平常一样定义包

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14531934

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档