使用mvc创建博客条目的快照/缩略图

Rif*_*faj 4 asp.net-mvc html-parsing asp.net-mvc-4

我需要从多个博客链接生成快照.

我所拥有的是像这样的文本列表" 报告: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
Run Code Online (Sandbox Code Playgroud)

我从我的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
Run Code Online (Sandbox Code Playgroud)

Ham*_*den 7

我从你的问题中理解的是,你需要一些链接的预览,我们可以在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);
Run Code Online (Sandbox Code Playgroud)

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