我正在关注Stephen Walther's指南,所有内容都会毫无错误地构建。但是,一旦我在Chrome中运行该应用程序,我就会收到以下错误消息:
Application Cache Error event: Failed to parse manifest http://localhost/website/Manifest.ashx
没有任何缓存。
根据我从here收集的内容,我的清单中有一个类型o。也许你可以看到我做错了并导致此错误消息。
Manifest.ashx:
<%@ WebHandler Language="C#" Class="JavaScriptReference.Manifest" %>
using System;
using System.Web;
namespace JavaScriptReference {
public class Manifest : IHttpHandler {
public void ProcessRequest(HttpContext context) {
context.Response.ContentType = "text/cache-manifest";
context.Response.WriteFile(context.Server.MapPath("Manifest.txt"));
}
public bool IsReusable {
get {
return false;
}
}
}
}
Manifest.txt:
CACHE MANIFEST
CACHE:
Images/img1.jpg
Images/img2.jpg
JScript.js
Default.aspx.vb
# Does Default.aspx.vb even need to be cached?
答案 0 :(得分:2)
TLDR:不要在清单中添加CACHE:条目,不要缓存代码隐藏文件,并确保在Web中注册了HttpHandler .Config
长版:
要使示例应用有效,您需要执行一些操作。首先,如上所述创建处理程序,C#中的示例是:
using System.Web;
namespace CacheTest
{
public class Manifest : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/cache-manifest";
context.Response.WriteFile(context.Server.MapPath("Manifest.txt"));
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
接下来,您需要在web.config中注册处理程序,如:
<configuration>
<system.web>
<httpHandlers>
<add verb="*" path="Manifest.ashx"
type="CacheTest.Manifest, CacheTest" />
</httpHandlers>
</system.web>
</configuration>
接下来在您网站的根目录中创建一个Manifest.txt并填充它。样本中不应该有CACHE:标题。工作样本可能如下所示:
CACHE MANIFEST
# v30
Default.aspx
Images/leaping-gorilla-logo.png
请注意,我们不会缓存文件后面的代码,只会缓存浏览器可能请求的实际资源的相对路径。最后,添加一个Default.aspx文件。忽略后面的代码但编辑标记,以便初始HTML标记引用HttpHandler,即完整标记:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CacheTest.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" manifest="Manifest.ashx">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
This is a sample offline app!
</div>
</form>
</body>
</html>
完成此操作后,您现在可以启动您的网站,在FireFox中浏览它,您将被要求获得脱机使用权限。或者,在Chrome中启动它,切换到开发人员工具,检查“资源”选项卡,您将能够看到已在“应用程序缓存”节点下加载的资源:
为了完整起见,您完成的代码结构将如下所示:
答案 1 :(得分:1)
错误“应用程序缓存错误事件:无法解析清单”可能是由文本文件格式化引起的。
我的部署脚本以Unicode格式生成清单文件。该文件在Chrome中看起来很好(当转到URL时),在在线验证器上验证,但在用作清单时会产生此错误。
要修复文件,只需在记事本中打开清单文件,然后转到“另存为”并选择UTF8。