1

背景:我正在使用 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"
                    };
                }
            }
        }
    }

我对这个解决方案一点也不满意。如果您可以对此进行改进,请提出建议。

4

1 回答 1

0

我可以从这篇博文中提出一个替代方案create your own token

总之,作者建议使用Web Essentials创建捆绑文件,然后创建一个 razor 助手来生成令牌,在这种情况下基于上次更改的日期和时间。

public static class StaticFile
{
    public static string Version(string rootRelativePath)
    {
        if (HttpRuntime.Cache[rootRelativePath] == null)
        {
            var absolutePath = HostingEnvironment.MapPath(rootRelativePath);
            var lastChangedDateTime = File.GetLastWriteTime(absolutePath);

            if (rootRelativePath.StartsWith("~"))
            {
                rootRelativePath = rootRelativePath.Substring(1);
            }

            var versionedUrl = rootRelativePath + "?v=" + lastChangedDateTime.Ticks;

            HttpRuntime.Cache.Insert(rootRelativePath, versionedUrl, new CacheDependency(absolutePath));
        }

        return HttpRuntime.Cache[rootRelativePath] as string;
    }
}

然后你可以像这样引用捆绑的文件......

@section scripts {
<script src="@StaticFile.Version("~/Scripts/app/myAppBundle.min.js")"></script>}

然后你就可以控制令牌,并且可以用它做你想做的事。

于 2014-07-07T09:51:49.510 回答