12

I have a url like example.com/page?a=1&ret=/user/page2.

I was using string.split('/') to figure out the paths but this case you can see it isn't very useful. How do i split the URL so i can get the page path?

jdphenix
  • 15,022
  • 3
  • 41
  • 74

10 Answers10

29

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"
jdphenix
  • 15,022
  • 3
  • 41
  • 74
dahlbyk
  • 75,175
  • 8
  • 100
  • 122
7

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.

jdphenix
  • 15,022
  • 3
  • 41
  • 74
Richard Anthony Hein
  • 10,550
  • 3
  • 42
  • 62
  • 1
    I would use nvc["ret"] rather than a numeric index, but yes HttpUtility.ParseQueryString is a good method to know. – dahlbyk Jun 23 '09 at 00:10
5

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.

Robin Day
  • 100,552
  • 23
  • 116
  • 167
3

You could load it into an URI object and get the Uri.AbsolutePath property.

heavyd
  • 17,303
  • 5
  • 56
  • 74
2

Is this an ASP.NET project? In your HttpHandler/Page you can simply use the Request object:

string path = HttpContext.Request.Path;

If you don't have an HttpContext, System.Uri gives you something similar:

string path = new Uri("example.com/page?a=1&ret=/user/page2").AbsolutePath;
jdphenix
  • 15,022
  • 3
  • 41
  • 74
Jeff Meatball Yang
  • 37,839
  • 27
  • 91
  • 125
1

This seems like a good case to use System.Uri:

Uri uri = new Uri("example.com/page?a=1&ret=/user/page2");
System.Windows.Forms.MessageBox.Show(
"Absolute URI: " + uri.AbsoluteUri + "\r\n" +
"Absolute Path: " + uri.AbsolutePath + "\r\n" +
"Local path: " + uri.LocalPath + "\r\n" +
"Host: " + uri.Host + "\r\n" +
"Port: " + uri.Port + "\r\n" +
"Query: " + uri.Query + "\r\n");
jdphenix
  • 15,022
  • 3
  • 41
  • 74
0

Check out System.Uri class. It'll parse out your url into fragments.

0

Have you considered using the UriBuilder... see stack over question 479799

Use that first then split up the .Path property

Community
  • 1
  • 1
bytebender
  • 7,371
  • 2
  • 31
  • 54
  • UriBuilder is only useful if you need to modify part of the URI after it is created. In this case, it seems the Uri would be read-only. – dahlbyk Jun 23 '09 at 00:12
0

you might also consider using the Routing API buit inside ASP.net 2.0 which will give you fine grained control over your URL routes

Rony
  • 9,331
  • 2
  • 22
  • 22
0

When you use Uri(), it has Segments which shows all parts of the url. If you need to return page2 section, just select the last segment:

string path = "http://example.com/page?a=1&ret=/user/page2";
Uri uri = new Uri(path);
Console.WriteLine(uri.Segments.LastOrDefault()); // returns page2
Amir978
  • 857
  • 3
  • 15
  • 38