Asp.net path compaction

Question :

Asp.net path compaction,

Answer :

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”.

Path.Combine didn’t fix it. Is there a function to clean this path up?

,

You could create a helper class which wrapped the UriBuilder class in System.Net

public static class UriHelper  {             public static string NormalizeRelativePath(string path)      {          UriBuilder _builder = new UriBuilder("http://localhost");          builder.Path = path;          return builder.Uri.AbsolutePath;      }  }  

which could then be used like this:

string url = "foo/bar/../bar/path.aspx";  Console.WriteLine(UriHelper.NormalizeRelativePath(url));  

It is a bit hacky but it would work for the specific example you gave.

Read More  Managed OleDB provider written in C#

EDIT: Updated to reflect Andrew’s comments.

That’s the answer Asp.net path compaction, Hope this helps those looking for an answer. Then we suggest to do a search for the next question and find the answer only on our site.

Disclaimer :

The answers provided above are only to be used to guide the learning process. The questions above are open-ended questions, meaning that many answers are not fixed as above. I hope this article can be useful, Thank you