我正在关注 Stephen Walther's 指南,一切都没有错误。但是,一旦我在 Chrome 中运行该应用程序,就会收到此错误消息:

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

并且没有缓存任何内容。

根据我从 here 收集到的信息,我的 list 中有一个类型 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?

最佳答案

TLDR: 不要在 list 中添加 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 中启动它,切换到开发人员工具,检查资源选项卡,您将能够看到已在应用程序缓存节点下加载的资源:

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

关于asp.net - 无法解析 list : Using asp. net,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11690896/

10-14 16:34
查看更多