In C#, is there any built-in way to "correct" URLs that have parent pathing in them? So, I want to take this:
/foo/bar/../baz.html
And turn it into...
/foo/baz.html
(The "bar" directory being negated by the ".." after it in the path.)
I've tried to hand-roll this, but the logic gets pretty ugly, pretty quickly. Consider something like:
/foo1/foo2/../foo3/foo4/foo5/../../bar.html
The path I headed down was to move through the URL, segment by segment, and start writing a new URL. I would only include segment 1 in the new URL, if segment 2 wasn't "..". But, in the case above, I need to "look ahead" and figure out how many parent paths I have coming.
I tried to use Path.GetFullPath, and it technically got it work, but, man, it's ugly. Fair warning: you may want to avert your eyes on this one:
Path.GetFullPath(myUrl).Replace(Path.GetFullPath(@"\"), "").Replace(@"\", "/") + "/";
GetFullPath returns a file system path from the "C:\" root, so you essentially have to replace that too, than convert the slashes, etc.
I can probably bang this out eventually (and my ugly code above technically works), but it strikes me that I can't be the first one to try this. Google did not help.
(The answer in another language would be helpful too -- at least it would show the logic.)