Created
February 25, 2012 14:02
-
-
Save jgable/1908669 to your computer and use it in GitHub Desktop.
MVC 3 Cache manifest result
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Web; | |
using System.Web.Mvc; | |
/// <summary> | |
/// Contains information about a cache manifest file to be generated. see http://www.html5rocks.com/en/tutorials/appcache/beginner/ | |
/// </summary> | |
public class CacheManifest | |
{ | |
public string Version { get; set; } | |
/// <summary> | |
/// Explicitly cached 'master entries'. | |
/// </summary> | |
public List<string> CacheFiles { get; set; } | |
/// <summary> | |
/// Resources that require the user to be online. | |
/// </summary> | |
public List<string> NetworkFiles { get; set; } | |
/// <summary> | |
/// Requests that will fall back if they cannot be accessed (can include wildcard) | |
/// NOTE: These url's will not be translated to local url's as they are expected to be full URL's. | |
/// </summary> | |
public Dictionary<string, string> FallbackFiles { get; set; } | |
public CacheManifest() | |
{ | |
this.Version = string.Empty; | |
this.CacheFiles = new List<string>(); | |
this.NetworkFiles = new List<string>(); | |
this.FallbackFiles = new Dictionary<string, string>(); | |
} | |
} | |
public class CacheManifestResult : ActionResult | |
{ | |
public CacheManifest Manifest { get; set; } | |
public CacheManifestResult() | |
{ | |
Manifest = new CacheManifest(); | |
} | |
public override void ExecuteResult(ControllerContext context) | |
{ | |
var resp = context.HttpContext.Response; | |
resp.ContentType = "text/cache-manifest"; | |
WriteResponseLine("CACHE MANIFEST", resp); | |
WriteResponseLine(string.Format("# {0}", Manifest.Version), resp); | |
WriteResponseLine(string.Empty, resp); | |
WriteResponseLine("CACHE:", resp); | |
foreach (var cacheLine in Manifest.CacheFiles) | |
{ | |
WriteResponseLine(GetContentUrl(cacheLine, context), resp); | |
} | |
if (Manifest.NetworkFiles != null && Manifest.NetworkFiles.Count > 0) | |
{ | |
WriteResponseLine(string.Empty, resp); | |
WriteResponseLine("NETWORK:", resp); | |
foreach (var networkLine in Manifest.NetworkFiles) | |
{ | |
WriteResponseLine(GetContentUrl(networkLine, context), resp); | |
} | |
} | |
if (Manifest.FallbackFiles != null && Manifest.FallbackFiles.Count > 0) | |
{ | |
WriteResponseLine(string.Empty, resp); | |
WriteResponseLine("FALLBACK:", resp); | |
var fallForm = "{0} {1}"; | |
foreach (var fallbackKeyVal in Manifest.FallbackFiles) | |
{ | |
// Not using the GetContentUrl here because most of the time these are actions. | |
var line = string.Format(fallForm, fallbackKeyVal.Key, fallbackKeyVal.Value); | |
WriteResponseLine(line, resp); | |
} | |
} | |
} | |
private static string GetContentUrl(string contentUrl, ControllerContext context) | |
{ | |
var url = new UrlHelper(context.RequestContext); | |
if (url.IsLocalUrl(contentUrl)) | |
{ | |
return url.Content(contentUrl); | |
} | |
return contentUrl; | |
} | |
private static void WriteResponseLine(string line, HttpResponseBase resp) | |
{ | |
resp.Write(line); | |
resp.Write(Environment.NewLine); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment