在MVC4中捆绑应用程序缓存

时间:2014-03-13 06:57:25

标签: asp.net-mvc asp.net-mvc-4 caching

我正在使用MVC项目。我使用应用程序缓存将整个页面存储到缓存中,以便应用程序甚至可以脱机使用。

这是我的离线控制器

   public class OfflineController : Controller
   {
    //
    // GET: /Offline/

    public ActionResult Index()
    {
        var manifestResult = new ManifestResult("1.0")
        {
            CacheResources = new List<string>() 
             {
                Url.Action("Index", "Home"),
                BundleTable.Bundles.ResolveBundleUrl("~/Content/css", true),
                BundleTable.Bundles.ResolveBundleUrl("~/bundles/modernizr", true),
                BundleTable.Bundles.ResolveBundleUrl("~/bundles/jquery",true),
                BundleTable.Bundles.ResolveBundleUrl("~/bundles/jqueryui", true),
                BundleTable.Bundles.ResolveBundleUrl("~/bundles/jqueryval",true),
                BundleTable.Bundles.ResolveBundleUrl("~/bundles/modernizr",true),
                BundleTable.Bundles.ResolveBundleUrl("~/Content/css",true),
                BundleTable.Bundles.ResolveBundleUrl("~/Content/themes/base/css")
             },
            NetworkResources = new string[] { "*" },
            FallbackResources = { { "Images/offline.jpg", "Images/offline.jpg" } } 
        };
        return manifestResult;
    }


}

我的家庭控制器就是这个

   public class HomeController : Controller
   {  
    public ActionResult Index()
    {
        ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";

        return View();
    }

    public ActionResult About()
    {
        ViewBag.Message = "Your app description page.";

        return View();
    }

    public ActionResult Contact()
    {
        ViewBag.Message = "Your contact page.";

        return View();
    }
}

这是我的类,它生成清单文件

 public class ManifestResult : FileResult
{
    public ManifestResult(string version)
        : base("text/cache-manifest")
    {
        Version = version;
        CacheResources = new List<string>();
        NetworkResources = new List<string>();
        FallbackResources = new Dictionary<string, string>();
    }
    public string Version { get; set; }
    public IEnumerable<string> CacheResources { get; set; }
    public IEnumerable<string> NetworkResources { get; set; }
    public Dictionary<string, string> FallbackResources { get; set; }

    protected override void WriteFile(HttpResponseBase response)
    {
        WriteManifestHeader(response);
        WriteCacheResources(response);
        WriteNetwork(response);
        WriteFallback(response);
    }

    private void WriteManifestHeader(HttpResponseBase response)
    {
        response.Output.WriteLine("CACHE MANIFEST");
        response.Output.WriteLine("#V" + Version ?? string.Empty);
    }
    private void WriteCacheResources(HttpResponseBase response)
    {
        response.Output.WriteLine("CACHE:");
        foreach (var cacheResource in CacheResources)
            response.Output.WriteLine(cacheResource);
    }
    private void WriteNetwork(HttpResponseBase response)
    {
        response.Output.WriteLine();
        response.Output.WriteLine("NETWORK:");
        foreach (var networkResource in NetworkResources)
            response.Output.WriteLine(networkResource);
    }
    private void WriteFallback(HttpResponseBase response)
    {
        response.Output.WriteLine();
        response.Output.WriteLine("FALLBACK:");
        foreach (var fallbackResource in FallbackResources)
            response.Output.WriteLine(fallbackResource.Key + " " + fallbackResource.Value);
    }

}

这是我遇到的错误 enter image description here

1 个答案:

答案 0 :(得分:1)

在我回答之前,对于那些刚刚来到这里的人来说,这是被引用的实现/文章:

http://www.infoq.com/articles/Offline-Web-Apps

我能够让这个工作得很好。您的实现与我的实现之间的一个区别是您在HTML标记中列出了清单:

<html manifest="/Offline">

..或者我猜猜你没有发布你的HTML。我不确定HTML标记是否会被正确解释。这是我正在使用和工作正常:

<html manifest="/Mobile/Manifest">

我的猜测是&#34;应用程序缓存错误事件:清单提取失败(-1)&#34;相当于404。

希望这有帮助...我发现自从你发布以来已经有一段时间了。