<p>I have an asp.net url path which is being generated in a web form, and is coming out something like "/foo/bar/../bar/path.aspx", and is coming out in the generated html like this too. It should be shortened to "/foo/bar/path.aspx". </p>

<p>Path.Combine didn't fix it. Is there a function to clean this path up? </p>

[{'answer_id': 54273, 'author': 'Compile This', 'author_id': 4048, 'author_profile': 'https://Stackoverflow.com/users/4048', 'pm_score': 3, 'selected': True, 'text': '<p>You could create a helper class which wrapped the UriBuilder class in System.Net</p>\n\n<pre><code>public static class UriHelper\n{       \n    public static string NormalizeRelativePath(string path)\n    {\n        UriBuilder _builder = new UriBuilder("http://localhost");\n        builder.Path = path;\n        return builder.Uri.AbsolutePath;\n    }\n}\n</code></pre>\n\n<p>which could then be used like this:</p>\n\n<pre><code>string url = "foo/bar/../bar/path.aspx";\nConsole.WriteLine(UriHelper.NormalizeRelativePath(url));\n</code></pre>\n\n<p>It is a bit hacky but it would work for the specific example you gave.</p>\n\n<p><strong>EDIT: Updated to reflect Andrew\'s comments.</strong></p>\n'}, {'answer_id': 54289, 'author': 'Ishmaeel', 'author_id': 227, 'author_profile': 'https://Stackoverflow.com/users/227', 'pm_score': 0, 'selected': False, 'text': '<p>Sarcastic\'s reply is so much better than mine, but if you were working with filesystem paths, my <strong>ugly hack below</strong> could turn out to be useful too. (Translation: I typed it, so I\'ll be damned if I don\'t post it :)</p>\n\n<p>Path.Combine just slaps two strings together, paying attention to leading or trailing slashes. As far as I know, the only Path method that does normalization is Path.GetFullPath. The following will give you the "cleaned up" version.</p>\n\n<pre><code>myPath = System.IO.Path.GetFullPath(myPath);\n</code></pre>\n\n<p>Of course, there is the small issue that the resulting path will be rooted and the forward slashes will be converted to back slashes (like "C:\\foo\\bar\\path.aspx"). But if you know the parent root of the original path, stripping out the root should not be a big problem.</p>\n'}, {'answer_id': 54297, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 1, 'selected': False, 'text': "<p>Whatever you do, don't use a static UriBuilder.  This introduces all sorts of potential race conditions that you might not detect until you are under heavy load.  </p>\n\n<p>If two different threads called UriHelper.NormalizeRelativePath at the same time, the return value for one could be passed back to the other caller arbitrarily.</p>\n\n<p>If you want to use UriBuilder to do this, just create a new one when you need it (it's not expensive to create).  </p>\n"}]
['https://Stackoverflow.com/questions/54227', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5599/']