Update:
I added the missing two variables to the repo code blush Sincere appologies about that – I was rushing out to pick up the kids and missed it when i quickly reviewed the post.
When i call the .NET framework’s MakeRelativeUri(…) method and the pass in a Uri which contains a file path .. and the file path has spaces in it .. then there’s a great disturbance in the Force, as if millions of voices suddenly cried out in terror, and were suddenly silenced.
Here’s my repo code. Appologies for this being an MSTest repo and not something a bit nicer like NUnit or XUnit.
[TestMethod]
public void SadPanda()
{
// Arrange.
var outputPath =
@"C:\Users\AAAAAAA.BBBBB\Documents\Visual Studio 2010\Projects\XWing\Code\CCCCCCCCCCC.XWing.Application.Web\content\shared\css\___sa.bundle.#.css";
var sourcePath =
@"C:\Users\AAAAAAA.BBBBB\Documents\Visual Studio 2010\Projects\XWing\Code\CCCCCCCCCCC.XWing.Application.Web\content\shared\css\home.css";
var sourcePathJussy =
@"C:\Projects\XWing\Code\CCCCCCCCCCC.XWing.Application.Web\content\shared\css\home.css";
// Added missing 2x vars *blush*
var sourceUri = new Uri(Path.GetDirectoryName(sourcePath) + "/", UriKind.Absolute);
var outputUri = new Uri(Path.GetDirectoryName(outputPath) + "/", UriKind.Absolute);
var relativePath = "../images/home-feature-bg.png";
var resolvedSourcePath = new Uri(sourceUri + relativePath, true);
// Act.
var resolvedOutput = outputUri.MakeRelativeUri(resolvedSourcePath);
// Assert.
Assert.IsTrue(resolvedOutput.Contains("XWing")); // :~(
}
Now if you look at the output it is something evil with the real path of the location, etc.
Now if we remove the SPACES from the paths, it now works 🙂
[TestMethod]
public void DoubleRaindbowUnicorns()
{
// Arrange.
var outputPath =
@"C:\Users\AAAAAAA.BBBBB\Documents\Visual Studio 2010\Projects\XWing\Code\CCCCCCCCCCC.XWing.Application.Web\content\shared\css\___sa.bundle.#.css";
var sourcePath =
@"C:\Users\AAAAAAA.BBBBB\Documents\Visual Studio 2010\Projects\XWing\Code\CCCCCCCCCCC.XWing.Application.Web\content\shared\css\home.css";
var sourcePathJussy =
@"C:\Projects\XWing\Code\CCCCCCCCCCC.XWing.Application.Web\content\shared\css\home.css";
outputPath = outputPath.Replace(" ", "-");
sourcePath = sourcePath.Replace(" ", "-");
// Added missing 2x vars *blush*
var sourceUri = new Uri(Path.GetDirectoryName(sourcePath) + "/", UriKind.Absolute);
var outputUri = new Uri(Path.GetDirectoryName(outputPath) + "/", UriKind.Absolute);
var relativePath = "../images/home-feature-bg.png";
var resolvedSourcePath = new Uri(sourceUri + relativePath, true);
// Act.
var resolvedOutput = outputUri.MakeRelativeUri(resolvedSourcePath);
// Assert.
Assert.IsFalse(resolvedOutput.Contains("XWing")); // Here yee! Woot, say I!
}
the output of this image resource is ../images/home-feature-bg.png (which by fluke is the same as it’s source path) .. and not the really long evil string.
Yes / no ?
Update 2:
Renamed the subject (to better reflect the answer).
You need to use the
System.IO.Path.Combine()function. What’s happening here is you are using aURIclass which cannot contain spaces, which would require the%20if it were a path for HTML.