大佬教程收集整理的这篇文章主要介绍了asp.net-mvc – 可以使用“Bundle.Include”(在ASP.NET MVC中使用的System.Web.Optimization)中抛出异常,而不是默认忽略丢失的文件?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经试过下面的代码,但它永远不会抛出异常…
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); } } } } }
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 ); } }
然后在配置中:
bundles.Add( new ScriptBundleWithException( "~/bundles/something" ) //...
以上是大佬教程为你收集整理的asp.net-mvc – 可以使用“Bundle.Include”(在ASP.NET MVC中使用的System.Web.Optimization)中抛出异常,而不是默认忽略丢失的文件?全部内容,希望文章能够帮你解决asp.net-mvc – 可以使用“Bundle.Include”(在ASP.NET MVC中使用的System.Web.Optimization)中抛出异常,而不是默认忽略丢失的文件?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。