asp.net - 相对于 HTML 中的绝对路径

标签 asp.net html relative-path absolute-path

我需要通过 URL 创建新闻通讯。为此,我:

  1. 创建一个WebClient
  2. 使用WebClient的方法DownloadData获取字节数组中的页面源;
  3. 从 source-html 字节数组中获取字符串并将其设置为时事通讯内容。

但是,我在路径方面遇到了一些麻烦。所有元素的来源都是相对的 (/img/welcome.png) 但我需要一个绝对来源,例如 http://www.example.com/img/welcome.png .

我该怎么做?

最佳答案

解决此任务的一种可能方法是使用 HtmlAgilityPack图书馆。

一些示例(修复链接):

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

关于asp.net - 相对于 HTML 中的绝对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2719353/

相关文章:

java - 从 Tomcat 上的相对路径读取文件

c# - 使用ajax和asp.net上传图片返回html

asp.net - Web应用程序的IIS7文件夹权限

javascript - 选择时如何更改菜单颜色?

html - 如何使语义用户界面中的固定顶部菜单不覆盖内容

javascript - 为移动站点 Jquery 和 CSS 创建 Accordion 菜单

java - 在 Ant 中,如何导入与声明构建文件相关的属性文件

css - 在 aspx 文件中使用 eval 动态更改 css

html - 使用媒体查询在设置的屏幕宽度和 Bootstrap 网格中关闭 css 规则

html - HTML 中的相对路径