简体   繁体   English

我如何错误地使用System.Uri和MakeRelativeUri(..)方法?

[英]How am I using the System.Uri incorrectly & MakeRelativeUri(..) method?

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. 我添加缺少的两个变量来回购代码脸红真诚appologies有关-我被冲出去接孩子,错过了它,当我迅速审查的职位。


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. 当我调用.NET Framework的MakeRelativeUri(...)方法并传入包含文件路径..且文件路径中带有空格的Uri时,.. Force受到极大干扰,好像数百万声音突然惊恐地响起,并突然保持沉默。

Here's my repo code. 这是我的回购代码。 Appologies for this being an MSTest repo and not something a bit nicer like NUnit or XUnit. 这种应用是MSTest回购,而不是像NUnit或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 :) 现在,如果我们从路径中删除SPACES,它现在就可以工作了:)

[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. 此图像资源的输出是../images/home-feature-bg.png (通过fluke,它与源路径相同)..而不是真正的长邪恶字符串。

Yes / no ? 是/否?

Update 2: 更新2:

Renamed the subject (to better reflect the answer). 重命名了主题(以更好地反映答案)。

You need to use the System.IO.Path.Combine() function. 您需要使用System.IO.Path.Combine()函数。 What's happening here is you are using a URI class which cannot contain spaces, which would require the %20 if it were a path for HTML. 这里发生的事情是您使用的URI类不能包含空格,如果它是HTML的路径,则需要%20

I think you are misunderstanding the purpose of the MakeRelativeUri() function. 我认为您误解了MakeRelativeUri()函数的目的。 It is supposed to be called on one absolute URI taking in another in order to return the relative URI which represents how to arrive at the second resource in context of the first. 为了返回表示在第一个绝对URI中如何到达第二个资源的相对URI,应该在一个绝对URI中调用另一个绝对URI。

Example: 例:

var cssFileUri = new Uri(@"C:\Temp\Spaces in this path\foo.css");
var imageFileUri = new Uri(@"C:\Temp\Images\bar.png");

var relativeUri = cssFileUri.MakeRelative(imageFileUri);
// The value of relativeUri is "../Images/bar.png"

So you can see how the relativeUri value gets us to the "bar.png" file from "foo.css". 因此,您可以看到relativeUri值如何使我们从“ foo.css”进入“ bar.png”文件。

In your example you'd pass in your CSS file path and a path to the image to get the value you have in relativePath . 在您的示例中,您将传入CSS文件路径和图像路径,以获取您在relativePath拥有的值。

In order to "undo" that (find the full path of an image in relation to a given CSS file) you construct a new Uri object: 为了“撤消”该操作(找到与给定CSS文件有关的图像的完整路径),请构造一个新的Uri对象:

var relativeImageUri = @"..\Image Folder\bar.png";
var cssFileUri = new Uri(@"C:\Some Path\Other Folder\foo.css");

var absoluteImageUri = new Uri(cssFileUri, relativeImageUri);
// The value of absoluteImageUri is "file:///C:/Some Path/Image Folder/bar.png"

So in order to find the resolvedOutput from your sample code: 因此,为了从示例代码中找到resolvedOutput

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 relativePath = "../images/home-feature-bg.png";

var outputUriBase = new Uri(outputPath);
var resolvedOutput = new Uri(outputUriBase, relativePath);

You can now use the resolvedOutput Uri object to access the "home-feature-bg.png" image relative to the output path. 现在,您可以使用resolvedOutput Uri对象为“家庭功能bg.png”图像相对于访问的输出路径。

All these handle spaces in the path without error. 所有这些都可以正确处理路径中的空格。

The

uriInstance.MakeRelativeUri() 

method seems to be corrupt. 方法似乎已损坏。 The resulting Uri will show escaped characters when called with ToString(), eg %20 for an empty space. 用ToString()调用时,生成的Uri将显示转义字符,例如%20表示空白。 Other Uri instances don't do so, not even relative ones. 其他Uri实例则不这样做,甚至相对实例也没有。

I wanted to use it for creating relative paths by subtracting the root path from a full path, then store a string from it in a DB 我想通过从完整路径中减去根路径来将其用于创建相对路径,然后将其中的字符串存储在数据库中

var baseUri = new Uri("http://myserver/pics")
var fullUri = new Uri("http://myserver/pics/myself/smile a.jpg")

var madeRelativeUri = baseUri.MakeRelativeUri(fullUri)

  // madeRelativeUri.ToString() = "myself/smile%20a.jpg"

var manualRelativeUri = new Uri(@"myself/smile b", UriKind.Relative)

  // manualRelativeUri.ToString() = "myself/smile b.jpg" // no escape

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM