asp.net-mvc – 可以使用“Bundle.Include”(在ASP.NET MVC中使用的System.We
发布时间:2020-05-23 18:44:09 所属栏目:asp.Net 来源:互联网
导读:如果缺少javascript或css文件,是否有可能收到异常? 我已经试过下面的代码,但它永远不会抛出异常… public class BundleConfig { public static void RegisterBundles(BundleCollection bundles) { .... bundles.Add(new ScriptBund
|
如果缺少javascript或css文件,是否有可能收到异常? 我已经试过下面的代码,但它永远不会抛出异常… 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);
}
}
}
}
}
解决方法我使用这个类而不是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 );
}
}
然后在配置中: bundles.Add( new ScriptBundleWithException( "~/bundles/something" ) //... (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 通过javascript从代码后面访问变量
- asp.net – 使用NuGet发布可执行文件和网站
- asp.net-mvc-3 – 被重定向到错误loginUrl – 帐户/登录,而
- asp.net-mvc – 如何锁定一个asp.net mvc动作?
- asp.net-web-api – 没有实体框架的WebAPI ODATA
- asp.net-mvc – LabelFor和TextBoxFor不生成相同的id
- asp.net下Cache 缓存操作类代码
- asp.net-mvc – 如何应用css类到mvccontrib网格
- asp.net-mvc – 如何在我的Asp.net Mvc中使用linq2sql存储库
- asp.net-mvc – 具有适当的敲除绑定的网格小部件
推荐文章
站长推荐
- asp.net-mvc – 实体框架中的字段最大长度
- asp.net-mvc – Internet Explorer缓存asp.netmv
- asp.net-mvc-3 – 用于ASP.NET MVC的Razor View
- asp.net-mvc – ASP.NET MVC是否使用常规工具箱控
- asp.net-web-api – 间歇性“无法加载类型”Syst
- asp.net-mvc-4 – Require.js优化vs asp.net mvc
- asp.net-mvc – 如何使用post或get来检查控制器是
- asp.net-mvc – Ninject.Extensions.Logging.nlo
- asp.net – 如何在主页面中包含CSS?
- asp.net – App_Start文件夹在ASP 4.5仅在WebApp
热点阅读
