背景:我正在使用 HTML 5 Offline App Cache 并动态构建清单文件。基本上,清单文件需要列出您的页面将请求的每个静态文件。当文件实际上是静态的时效果很好,但我在 中使用捆绑和缩小System.Web.Optimization
,所以我的文件不是静态的。
当在 DEBUG 中加载符号时(即在 VS 中调试),然后从 MVC 视图调用实际的物理文件。但是,在发布模式下,它会调用一个看起来像这样的虚拟文件:/bundles/scripts/jquery?v=FVs3ACwOLIVInrAl5sdzR2jrCDmVOWFbZMY6g6Q0ulE1
所以我的问题是:如何在代码中获取该 URL 以将其添加到离线应用程序清单?
我试过了:
var paths = new List<string>()
{
"~/bundles/styles/common",
"~/bundles/styles/common1024",
"~/bundles/styles/common768",
"~/bundles/styles/common480",
"~/bundles/styles/frontend",
"~/bundles/scripts/jquery",
"~/bundles/scripts/common",
"~/bundles/scripts/frontend"
};
var bundleTable = BundleTable.Bundles;
foreach (var bundle in bundleTable.Where(b => paths.Contains(b.Path)))
{
var bundleContext = new BundleContext(this.HttpContext, bundleTable, bundle.Path);
IEnumerable<BundleFile> files = bundle.GenerateBundleResponse(bundleContext).Files;
foreach (var file in files)
{
var filePath = file.IncludedVirtualPath.TrimStart(new[] { '~' });
sb.AppendFormat(formatFullDomain, filePath);
}
}
除了替换GenerateBundleResponse()
为EnumerateFiles()
, 但它总是返回原始文件路径。
我也愿意接受替代实施建议。谢谢。
更新:(7/7/14 13:45)
除了下面的答案,我还添加了这个 Bundles Registry 类来保存所需静态文件的列表,以便它在所有浏览器中以调试模式工作。(见下面的评论)
public class Registry
{
public bool Debug = false;
public Registry()
{
SetDebug();
}
[Conditional("DEBUG")]
private void SetDebug()
{
Debug = true;
}
public IEnumerable<string> CommonScripts
{
get
{
if (Debug)
{
return new string[]{
"/scripts/common/jquery.validate.js",
"/scripts/common/jquery.validate.unobtrusive.js",
"/scripts/common/knockout-3.1.0.debug.js",
"/scripts/common/jquery.timepicker.js",
"/scripts/common/datepicker.js",
"/scripts/common/utils.js",
"/scripts/common/jquery.minicolors.js",
"/scripts/common/chosen.jquery.custom.js"
};
}
else
{
return new string[]{
"/scripts/common/commonbundle.js"
};
}
}
}
}
我对这个解决方案一点也不满意。如果您可以对此进行改进,请提出建议。