Arjun_TECH
Arjun_TECH

Reputation: 383

How does a browser resolves/constructs an absolute url from a relative one

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"
}

Upvotes: 0

Views: 83

Answers (2)

smolchanovsky
smolchanovsky

Reputation: 1863

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");

Upvotes: 1

Arjun_TECH
Arjun_TECH

Reputation: 383

This could be achieved by following the question here:

static string ToAbsoluteUrl(string a, string b)
{   
    return new Uri(new Uri(a), b).ToString();
}

Upvotes: 0

Related Questions