【问题标题】:Html 5 manifest is not working by iFrame correctlyHtml 5 清单无法通过 iFrame 正确工作
【发布时间】:2014-03-18 06:53:06
【问题描述】:

我有一个包含多个 C# 项目的应用程序,如下所示

  • CRM.DomainLogic
  • CRM.Web
  • Warehouse.DomainLogic
  • Warehouse.Web
  • ,...

每个 web 项目(例如 CRM.Web)都有自己的“html 视图”和“js 控制器” 还有其他几种静态文件。

为了使部署更容易,我想使用 Html5 清单。

因此,为了使跨项目的单独部署成为可能,我使用了 iframe。 由于CRM.Web的变化,客户将获得CRM文件,无需再次下载Warehouse.Web文件!

步骤:

1- 我有一个返回 Web 程序集的所有名称的 Web API 方法,例如 CRM.Web 和 Warehouse.Web

2- 我有另一个 Web API 方法,它获取程序集名称作为参数并返回指向位于该项目上的文件的清单文件内容。

public HttpResponseMessage GetManifestByAssemblyName(String assemblyName)

此处省略代码。

response.Content = new StringContent(manifestBody.ToString(), Encoding.UTF8, "text/cache-manifest");

3- 在客户端,我为每个程序集创建一个新的 iFrame,并将 src 设置为另一个返回 html 正文的 Web api 方法,它的清单被分配给返回清单正文的 WebAPI 方法的地址(GetManifestByAssemblyName)

    String result = String.Format
        (@"<html manifest='{0}'> </html>", "/api/AppManifest/GetManifestByAssemblyName?assemblyName=" + assemblyName + ".manifest");

    response.Content = new StringContent(result, UTF8Encoding.Default, "text/html");

iFrames的代码:

var htmlPageUrl = "/api/AppManifest/GetHtmlContainerForManifestByName?assemblyName=" + name;

                    var iFrame = document.createElement("iFrame");
                    iFrame.setAttribute("src", htmlPageUrl);
                    iFrame.setAttribute("sandbox", "allow-same-origin");
                    iFrame.setAttribute("seamless", "seamless");
                    document.body.appendChild(iFrame);

当我运行应用程序时,它会获取程序集名称,然后创建 iFrame,每个 iFrame 都会自动获取自己的清单。

但是window.applicationCache.status 为0,表示没有缓存。

当我进入资源页面时,我可以看到以下内容:

但是当我请求其中一个被缓存的文件时,该请求不会使用缓存。

我知道我在网络上的工作没有参考,这完全基于我的想法,我知道这里可能会出现一些安全限制,但是有什么解决方案可以解决问题吗?

提前致谢。

注意:You can download sample application source code here.

然后按Ctrl+S保存文件,并先阅读Index.html注释。

【问题讨论】:

    标签: c# html iframe cache-manifest


    【解决方案1】:

    我尝试了您的示例项目。我不得不将sandbox 属性更改为allow-scripts,否则Chrome 会阻止来自iFrame 的JS 执行。

    除此之外,我认为您的缓存工作正常。请参见下面的屏幕截图。

    我认为您感到困惑的根本原因是您正在检查没有清单的主文档的 appCache 状态。清单适用于 iFrame 中加载的文档。所以在 Chrome 中,如果你切换到单个 iFrame 并检查状态,它会显示Idle (1),这意味着缓存处于活动状态!见下文:

    希望这会有所帮助!

    【讨论】:

    • 在控制台上运行以下命令:$.ajax('/api/AppManifest/GetResByName?resName=CRM.Web.Models.Customer.js');不幸的是,它向服务器发送请求,而不是从缓存的清单中加载。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-20
    • 2013-03-04
    • 2018-11-14
    • 2014-01-19
    • 1970-01-01
    相关资源
    最近更新 更多