我已经创建了一个原型单页面网络应用程序,它使用捆绑我的javascript和css资源。我希望能够更好地控制客户端浏览器如何缓存资源,因此我尝试实现缓存清单。 但是我无法在清单控制器中获取捆绑网址以匹配我的网页中使用的捆绑网址。(这导致我的资源无法正常处理)
这是我的页面:
<!DOCTYPE html>
<html manifest="/manifest/appcache">
<head>
<meta charset="utf-8" />
<title>App</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />
@Styles.Render("~/bundles/css")
</head>
<body>
<div id="viewContainer"></div>
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@Scripts.Render("~/bundles/mustache")
@Scripts.Render("~/bundles/app")
</body>
</html>
这是我的appcache清单控制器:
[NoCache]
public class ManifestController : Controller
{
// GET: Manifest
public ActionResult AppCache()
{
var sb = new StringBuilder();
sb.AppendLine("CACHE MANIFEST");
sb.AppendLine("");
sb.AppendLine("# version " + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version);
sb.AppendLine("");
sb.AppendLine("CACHE:");
sb.AppendLine(Styles.Url("/bundles/css").ToString());
sb.AppendLine(Scripts.Url("/bundles/jquery").ToString());
sb.AppendLine(Scripts.Url("/bundles/bootstrap").ToString());
sb.AppendLine(Scripts.Url("/bundles/mustache").ToString());
sb.AppendLine(Scripts.Url("/bundles/app").ToString());
return Content(sb.ToString(), "text/cache-manifest");
}
}
渲染时,我的页面如下所示:
<!DOCTYPE html>
<html manifest="/manifest/appcache">
<head>
<meta charset="utf-8" />
<title>App</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="/favicon.ico" rel="shortcut icon" type="image/x-icon" />
<link href="/bundles/css?v=05foeD1ouw8_HkR2BuZ1LeJIjvM0ZzqvfJQwDkNrBRE1" rel="stylesheet"/>
</head>
<body>
<div id="viewContainer"></div>
<script src="/bundles/jquery?v=rD9yxcIfC-_zwpaJ_9UPbUY1Niam5dFE8OFiugxkBeM1"></script>
<script src="/bundles/bootstrap?v=oTDoDDX3Lkc7trL2ZkC3RJsRItMEUUqdj_UlAAmiNno1"></script>
<script src="/bundles/mustache?v=W_BIas2vDP9wDqc-bEsDBkx1wQ0fDqYjeZKFVNXbZe01"></script>
<script src="/bundles/app?v=3Mv6xc52lUG5p86bUG4RvyJWAbSPxRjHfgUd3W4MMw81"></script>
</body>
</html>
我呈现的appcache清单看起来像这样:
CACHE MANIFEST
# version 0.0.0.16497
CACHE:
/bundles/css
/bundles/jquery
/bundles/bootstrap
/bundles/mustache
/bundles/app
正如您所看到的,清单中的资源URL与页面中的资源URL不匹配,因为缓存破坏查询字符串(存在于页面中,但不在清单中)。这导致资源无法正确缓存。所以我的问题是(是):