Skip to content

Instantly share code, notes, and snippets.

@kobi
Created March 15, 2012 11:08
Show Gist options
  • Save kobi/2043685 to your computer and use it in GitHub Desktop.
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
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