Created
March 15, 2012 11:08
-
-
Save kobi/2043685 to your computer and use it in GitHub Desktop.
Workaround for Cassette precompiledManifest issue with application cirtual path: https://groups.google.com/forum/?fromgroups#!topic/cassette/pNIxVczsHEg
This file contains 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.Web; | |
using System.Text.RegularExpressions; | |
using System.Xml.Linq; | |
public static class CassetteHelper | |
{ | |
private static readonly Regex EnsurePathRegex = new Regex(@"((?:href|src)\s*=\s*"")/(_cassette/)", | |
RegexOptions.Compiled); | |
public static string EnsureVirtualPath(string html, string virtualPath) | |
{ | |
return EnsurePathRegex.Replace(html, m => String.Concat(m.Groups[1].Value, virtualPath, m.Groups[2].Value)); | |
} | |
internal static void FixCassetteXmlFile(HttpServerUtility server) | |
{ | |
try | |
{ | |
string xmlFile = server.MapPath("~/App_Data/cassette.xml"); | |
string virtualPath = HttpRuntime.AppDomainAppVirtualPath; | |
if (String.IsNullOrEmpty(virtualPath) || virtualPath.Equals("/")) | |
return; | |
if (!virtualPath.EndsWith("/")) | |
virtualPath += "/"; | |
XDocument doc = XDocument.Load(xmlFile); | |
if (doc.Root == null) | |
return; | |
var htmlElements = doc.Root.Descendants("Html"); | |
foreach (var htmlElement in htmlElements) | |
{ | |
htmlElement.Value = EnsureVirtualPath(htmlElement.Value, virtualPath); | |
} | |
doc.Save(xmlFile); | |
} | |
catch | |
{ | |
//don't really care, but don't fail application start. | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment