asp.net-mvc – Asp.Net MVC捆绑,最好的方式来检测丢失的文件
发布时间:2020-05-30 07:42:37 所属栏目:asp.Net 来源:互联网
导读:我刚刚追赶了一个由于缺少的 javascript文件造成的错误,它是默默无声的. 文件的最小化版本存在但不是完整版本,一个链接不会呈现在客户端(我期待的),但是我也没有得到例外.我想知道文件是否不存在. (只是为了清楚,捆绑包没有尝试包括最小化的版本,它试图包括完
|
我刚刚追赶了一个由于缺少的 javascript文件造成的错误,它是默默无声的. 文件的最小化版本存在但不是完整版本,一个链接不会呈现在客户端(我期待的),但是我也没有得到例外.我想知道文件是否不存在. (只是为了清楚,捆绑包没有尝试包括最小化的版本,它试图包括完整版本,但最小化版本存在于脚本目录中) 我必须写一些自定义的东西来检测这个,或者MVC有什么内置的来报告? 谢谢 解决方法我想起了对Bundle使用以下扩展方法:public static class BundleHelper
{
[Conditional("DEBUG")] // remove this attribute to validate bundles in production too
private static void CheckExistence(string virtualPath)
{
int i = virtualPath.LastIndexOf('/');
string path = HostingEnvironment.MapPath(virtualPath.Substring(0,i));
string fileName = virtualPath.Substring(i + 1);
bool found = Directory.Exists(path);
if (found)
{
if (fileName.Contains("{version}"))
{
var re = new Regex(fileName.Replace(".",@".").Replace("{version}",@"(d+(?:.d+){1,3})"));
fileName = fileName.Replace("{version}","*");
found = Directory.EnumerateFiles(path,fileName).Where(file => re.IsMatch(file)).FirstOrDefault() != null;
}
else // fileName may contain '*'
found = Directory.EnumerateFiles(path,fileName).FirstOrDefault() != null;
}
if (!found)
throw new ApplicationException(String.Format("Bundle resource '{0}' not found",virtualPath));
}
public static Bundle IncludeExisting(this Bundle bundle,params string[] virtualPaths)
{
foreach (string virtualPath in virtualPaths)
CheckExistence(virtualPath);
return bundle.Include(virtualPaths);
}
public static Bundle IncludeExisting(this Bundle bundle,string virtualPath,params IItemTransform[] transforms)
{
CheckExistence(virtualPath);
return bundle.Include(virtualPath,transforms);
}
}
这样你就不必明确地调用你的帮助方法PreCheck().它还支持ASP.NET的通配符{version}和*: bundles.Add(new ScriptBundle("~/test")
.IncludeExisting("~/Scripts/jquery/jquery-{version}.js")
.IncludeExisting("~/Scripts/lib*")
.IncludeExisting("~/Scripts/model.js")
); (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 获取ASP.Net MVC中任何文件的完整网址
- asp.net-mvc – thinktecture identityserver v3
- asp.net-mvc – 在MVC 3中回发到控制器操作后,ViewModel集合
- 从客户端调用asp.net ajax服务器控件的公共函数
- asp.net-mvc – 重新安装Microsoft Visual Studio Asp.net
- asp.net-mvc – WebApiConfig.cs和RouteConfig.cs之间的区别
- asp.net-mvc – Jquery AJAX成功没有被Coded UI测试项目触发
- asp.net-mvc-3 – DropDownListFor中的ViewBag属性值,而不是
- asp.net-mvc – .NET MVC MultiSelectList和选定的值
- asp.net-mvc – 使用@ Html.Partial渲染usercontrol(cshtml
推荐文章
站长推荐
- asp.net – html页面的ASP服务器统计信息
- asp.net-mvc-3 – 在html标签中添加html输入
- asp.net-mvc – 在IIS Express上设置SMTP
- asp.net-mvc – 如何使用MsBuild MsDeployPublis
- asp.net-mvc – 如何使用MVCSiteMap进行隐式本地
- asp.net-mvc-2 – 读取HTTP请求自定义标头
- 发布一款层次下拉列表控件
- asp.net-mvc – Url.Action映射Route属性的错误链
- asp.net – 空网站和git
- asp.net-core-mvc – 什么是Asp.Net Core MVC中的
热点阅读
