Relative to absolute paths in HTML

absolute-path, asp.net, html, relative-path

Solution

One of the possible ways to resolve this task is the use the HtmlAgilityPack library.

Some example (fix links):

WebClient client = new WebClient();
byte[] requestHTML = client.DownloadData(sourceUrl);
string sourceHTML = new UTF8Encoding().GetString(requestHTML);

HtmlDocument htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(sourceHTML);

foreach (HtmlNode link in htmlDoc.DocumentNode.SelectNodes("//a[@href]"))
{
    if (!string.IsNullOrEmpty(link.Attributes["href"].Value))
    {
        HtmlAttribute att = link.Attributes["href"];
        att.Value = this.AbsoluteUrlByRelative(att.Value);
    }
}

Problem

I need to create a newsletters by URL. To do that, I: - Create a `WebClient`. - Use WebClient's method `DownloadData` to get a source of page in byte array; - Get string from the source-html byte array and set it to the newsletter content. However, I have some troubles with paths. All elements' sources were relative (`/img/welcome.png`) but I need an absolute one, like http://www.example.com/img/welcome.png. How can I do this?

Original source