0

A website showing url in browser addressbar : www.something.com/abc/def/ghi . When you view source any websites, anchor tags have relative hrefs like href="../jkl/mno" When you click the link, how does browser determine that it has to load www.something.com/abc/jkl/mno ?

For example: browse https://alibabagroup.com/en/global/home --> Expand Investor Relations --> Inspect source of any link. e.g. a href="../ir/home" --> Click the link --> Browser resolves this to "https://www.alibabagroup.com/en/ir/home" by replacing the "global/home" portion

Also, it would be a great help if somebody knows how to achieve this programatically in C sharp ? Some function like below:

string ToAbsoluteUrl("https://alibabagroup.com/en/global/home", "../ir/home")
{
    // Outputs as "https://alibabagroup.com/en/ir/home"
}
Arjun_TECH
  • 343
  • 1
  • 4
  • 16

2 Answers2

1
string ToAbsoluteUrl(params string[] pathParts)
{
    return new Uri(Path.Combine(pathParts)).ToString();
}

use it this way:

ToAbsoluteUrl("https://alibabagroup.com/en/global/home", "../ir/home");
smolchanovsky
  • 1,775
  • 2
  • 15
  • 29
  • Thanks friend for the help, but its not working as Path.GetFullPath is of System.IO which may not be used to combine url paths. Currently it's giving exception "The given path's format is not supported." You may check here : https://dotnetfiddle.net/kXphgX – Arjun_TECH Oct 10 '18 at 06:56
  • @arjun-tech I fixed my answer – smolchanovsky Oct 10 '18 at 07:25
0

This could be achieved by following the question here:

static string ToAbsoluteUrl(string a, string b)
{   
    return new Uri(new Uri(a), b).ToString();
}
Arjun_TECH
  • 343
  • 1
  • 4
  • 16