URL split in C#?

Assuming you mean you want to get the "page2" bit:

 var ub = new UriBuilder("example.com/page?a=1&ret=/user/page2");
 NameValueCollection nvc = HttpUtility.ParseQueryString(ub.Query);
 string page = nvc[nvc.Count - 1]; // gets "/user/page2"

Then you'll have to use split on the rest.

Edit: Well, you could use System.IO.Path.GetFileNameWithoutExtension(page) to return "page2", but I am not sure it feels right to me.

System.IO.Path.GetFileNameWithoutExtension("example.com/page?a=1&ret=/user/page2") returns "page2" as well.


If you make a System.Uri object from your string, it will have several properties for different parts of the path:

string path = "http://example.com/page?a=1&ret=/user/page2";
Uri uri = new Uri(path);
Console.WriteLine(uri.AbsolutePath); // Prints "/page"

The Request.Url (Uri) object has a lot of useful properties relating to the path. It can give you the entire QueryString to take off of the full url if that's what you're after?

You can also perform a Server.MapPath on the page itself and then use the FileInfo object to view various parts of the file itself.

Tags:

C#

Asp.Net