user3347535
user3347535

Reputation: 11

Extracting segment path parameters from url

I have been trying to figure out how I could extract the segment path parameters from a REST url.
We know that REST url parameters can be represented in two ways:

  1. As part of the path
  2. As a query parameter.

When a user clicks on a link from our website we capture the link and the subsequent path traversed using Google Api and save it in our database as three parts:

  1. The base url
  2. The segment path parameters (if any)
  3. The query parameters (if any).

For example: http://www.test.com/article/12345?order=2

is seperated into:

baseurl : http://www.test.com/article
segment path parameters : 12345
query parameters : order=2

I am aware of how I can separate the segments out of a url, but my question is how do I know if in a given url a segment is a parameter or not. For example in the above example "12345" could be a REST parameter or simply part of the path. How does one distinguish that

Upvotes: 1

Views: 1660

Answers (1)

Habib
Habib

Reputation: 223247

Use Uri class:

Uri uri = new Uri(@"http://www.test.com/article/12345?order=2");

foreach (var segment in uri.Segments)
{
    Console.WriteLine(segment);
}

and you will get:

/
article/
12345

uri.Query would give you ?order=2

Upvotes: 3

Related Questions