0

I have a URL like this:

http://www.example.com/Catalog/Category/Detail 
http://www.example.com/Catalog/Products/12 

Now, I want to extract the /Catalog/Category/Detail and /Catalog/Products/12 part so I can append with some other base url. How can I do that easily?

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
john doe
  • 9,220
  • 23
  • 91
  • 167
  • Have a look at the [URI class](http://msdn.microsoft.com/en-us/library/system.uri(v=vs.110).aspx) and the [PathAndQuery](http://msdn.microsoft.com/en-us/library/system.uri.pathandquery(v=vs.110).aspx) property – MattC Oct 06 '14 at 15:44
  • 1
    Have you put any effort into this yourself? A quick glance at the [Uri class documentation](http://msdn.microsoft.com/en-us/library/system.uri(v=vs.110).aspx) should get you started. – mason Oct 06 '14 at 15:44

2 Answers2

1

Use Uri class, and use Uri.LocalPath property like:

Uri uri = new Uri("http://www.example.com/Catalog/Category/Detail");
Console.WriteLine(uri.LocalPath); // /Catalog/Category/Detail
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Habib
  • 219,104
  • 29
  • 407
  • 436
  • @johndoe And then to answer the question of how to append the `LocalPath` to the base, you can use an overload of the `Uri` constructor as answered in [Path.Combine for Urls?](http://stackoverflow.com/questions/372865/path-combine-for-urls). – mason Oct 06 '14 at 16:07
0
var segments = new Uri("http://www.example.com/Catalog/Category/Detail").Segments;

This will return

/
Catalog/
Category/
Detail
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
EZI
  • 15,209
  • 2
  • 27
  • 33
  • 3
    This doesn't really answer the question. – mason Oct 06 '14 at 15:46
  • Bloody negative voters... go read the guidelines... whilst I admit it only partially answers the question, how to get it easily, it does not show how to append stuff to it easily... it is nonetheless helpful and so does not deserve -1... +1 from me to cancel out the negativity. – Paul Zahra Oct 06 '14 at 15:50
  • 2
    @PaulZahra No, this returns the segments. But then what? How do you append them to the base path from there? Why would you ever bother doing this instead of using `Uri.LocalPath`? – mason Oct 06 '14 at 15:51