加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 使用mvc创建博客条目的快照/缩略图

发布时间:2020-12-16 06:32:14 所属栏目:asp.Net 来源:网络整理
导读:我需要从多个博客链接生成快照. 我所拥有的是这样的文本列表 “报告:Twitter将于本月http://on.mash.to/10L1v49通过@mashable发布音乐发现应用” 我想将链接显示为博客的快照,然后在我的视图中显示其文本.或者至少我需要将图片附加到博客上. 使用facebook调
我需要从多个博客链接生成快照.

我所拥有的是这样的文本列表
“报告:Twitter将于本月http://on.mash.to/10L1v49通过@mashable发布音乐发现应用”

我想将链接显示为博客的快照,然后在我的视图中显示其文本.或者至少我需要将图片附加到博客上.

使用facebook调试,http://developers.facebook.com/tools/debug,我得到了这个..

fb:app_id:  122071082108
og:url: http://mashable.com/2013/03/13/twitter-music-app/
og:type:    article
og:title:   Report: Twitter Will Release Music Discovery App This Month
og:image:   
og:description: Twitter is planning to release a standalone music app for iOS   called Twitter Music as soon as the end of this month,according to CNET. CNET reports that Twitter Music will help...
og:site_name:   Mashable
og:updated_time:    1363267654

我尝试使用c#代码中的相同链接,访问带参数’q’的链接作为我想要的链接.我得到了与回复相同的HTML,但我无法找到相关的图像,因为它对于不同的链接来说是不同的.

任何人都可以建议一个更好的方法来做到这一点在mvc?

我在控制器中访问facebook调试的代码:

var client = new RestClient
            {
                BaseUrl = "http://developers.facebook.com/tools/debug/og/object"
            };
            var request = new RestRequest
            {
                DateFormat = DataFormat.Xml.ToString(),Resource = "Add",Method = Method.GET
            };
            request.AddParameter("q","http://on.mash.to/10L1v49");

            IRestResponse response = client.Execute(request);
            var content = response.Content; // raw content as string

解决方法

我从你的问题中理解的是,你需要一些链接的预览,我们可以在facebook共享区域粘贴一些链接.

Facebook调试方法返回一个html页面,其中包含您给出的链接中的博客条目图像.

使用HtmlAgilityPack来解析从facebook调试返回的html

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
        doc.LoadHtml(content);
        HtmlNode root = doc.DocumentNode;
        var imageurl = doc.DocumentNode.SelectNodes("//img/@src").LastOrDefault();
        string imagesrc = imageurl.OuterHtml.ToString();
        int start = imagesrc.IndexOf("url=");
        int to = imagesrc.IndexOf(""",start + "url=".Length);
        string s = imagesrc.Substring(
                       start + "url=".Length,to - start - "url=".Length);
        string a = Uri.UnescapeDataString(s);

并且…你有你的博客条目的形象.可以修改相同的功能以退出博客条目的标题,描述和更新时间.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读