无法解析清单:使用ASP.NET
-
12-12-2019 - |
题
我追随斯蒂芬沃尔特的指南,一切都在没有错误。但是,一旦我在Chrome中运行应用程序,我就会收到此错误消息:
Application Cache Error event: Failed to parse manifest http://localhost/website/Manifest.ashx
.
而且没有缓存。
从这里,我的清单中有一个类型的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:不要添加缓存:输入清单中的条目,不要缓存代码文件,并确保在web.config中注册了httphandler
long版本:
您需要做一些事情来制作样本应用程序。首先,如上所述,创建处理程序,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 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中,切换到开发人员工具,选中“资源”选项卡,您将能够查看已加载在应用程序缓存节点下的资源:
而言,完成的代码结构看起来像:
其他提示
错误“application cache错误事件:无法解析清单”,可以通过格式化文本文件来引起。
我的部署脚本在Unicode中生成了清单文件。该文件在Chrome(转到URL时)罚款,在在线验证器上验证,但在用作清单时会生成此错误。
要修复文件,只需在记事本中打开清单文件,然后转到“保存为”并选择UTF8。不隶属于 StackOverflow