3

我遵循斯蒂芬沃尔特的指南,一切都没有错误。但是,一旦我在 Chrome 中运行该应用程序,就会收到以下错误消息:

Application Cache Error event: Failed to parse manifest http://localhost/website/Manifest.ashx

并且没有任何东西被缓存。

根据我从这里收集到的信息,我的清单中有一个 type-o。也许您可以看到我做错了什么并导致此错误消息。

清单.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;
            }
        }
    }
}

清单.txt:

CACHE MANIFEST

CACHE:
Images/img1.jpg
Images/img2.jpg
JScript.js
Default.aspx.vb 
# Does Default.aspx.vb even need to be cached?
4

2 回答 2

2

TLDR:不要在清单中添加 CACHE: 条目,不要缓存代码隐藏文件并确保在 Web.Config 中注册了 HttpHandler

长版:

要使示例应用程序正常工作,您需要做一些事情。首先你像上面一样创建你的处理程序,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 中启动它,切换到开发人员工具,检查 Resources 选项卡,您将能够看到已在 Application Cache 节点下加载的资源:

在 Google Chrome 中运行的离线应用程序

为了完整起见,您完成的代码结构将如下所示:

在此处输入图像描述

于 2013-10-17T19:47:03.517 回答
1

错误“应用程序缓存错误事件:无法解析清单”可能是由文本文件的格式引起的。

我的部署脚本以 Unicode 生成清单文件。该文件在 Chrome 中看起来很好(访问 URL 时),在在线验证器上进行了验证,但在用作清单时会生成此错误。

要修复文件,只需在记事本中打开清单文件,然后转到“另存为”并选择 UTF8。

于 2015-04-14T05:51:49.753 回答