2015-06-03 150 views
1

我试图阅读和处理一个WordPress的博客上我的MVC4网站。我在这里跟着这个例子,但我收到以下错误:Read rss feeds with c# mvc4阅读与MVC4的RSS订阅

错误:编译器错误消息:CS1061:“MyWebsites.Models.WordPressRSS”不包含“的RSSFeed”的定义,并没有扩展方法“的RSSFeed”接受类型 'MyWebsites.Models.WordPressRSS' 的第一个参数可以找到(是否缺少using指令或程序集引用?)

模型

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Web; 
using System.Xml.Linq; 

namespace MyWebsites.Models 
{ 
    public class WordPressRSS 
    { 
     public string Title { get; set; } 
     public string Description { get; set; } 
     public string Link { get; set; } 
     public string PubDate { get; set; } 
    } 

    public class ReadWordPressRSS 
    { 
     public static List<WordPressRSS> GetFeed() 
     { 
      var client = new WebClient(); 
      client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)"); 
      var xmlData = client.DownloadString("https://blog.wordpress.com/feed/"); 

      XDocument xml = XDocument.Parse(xmlData); 

      var Feed = (from story in xml.Descendants("item") 
            select new WordPressRSS 
            { 
             Title = ((string)story.Element("title")), 
             Link = ((string)story.Element("link")), 
             Description = ((string)story.Element("description")), 
             PubDate = ((string)story.Element("pubDate")) 
            }).Take(10).ToList(); 

      return Feed; 
     } 
    } 

    public class GetRSSFeed 
    { 
     public List<WordPressRSS> RSSFeed { get; set; } 
    } 
} 

控制器

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using System.Xml; 
using MyWebsites.Models; 


namespace MyWebsites.Controllers 
{ 
    public class BlogController : Controller 
    { 
     // 
     // GET: /Blog/ 

     public ActionResult Index() 
     { 
      GetRSSFeed model = new GetRSSFeed(); 

      model.RSSFeed = ReadWordPressRSS.GetFeed(); 
      return View(model); 
     } 



    } 
} 

查看

@model MyWebsites.Models.GetRSSFeed 

@{ 
    ViewBag.Title = "Blog"; 
} 

<div class="container"> 

    @foreach (var item in Model.RSSFeed) 
    { 
     @item.RSSFeed.FirstOrDefault().Title <br /> 
     @Html.Raw(item.RSSFeed.FirstOrDefault().Description) <br /> 
     @Convert.ToDateTime(item.RSSFeed.FirstOrDefault().PubDate) <br /> 
     @item.RSSFeed.FirstOrDefault().Link <br /> 
     <br /><br /> 
    } 

</div> <!-- container --> 

我觉得我在想念的东西超级简单,但我不能为我的生活解析此引用。帮助表示赞赏。

回答

1

我认为问题在于您的看法。

在for each loop中,item指的是WordPressRSS项目而不是列表。

尝试直接引用属性。

@item.Title 

而不是

@item.RSSFeed.FirstOrDefault().Title 
+0

也做到了,我知道这是愚蠢的东西。非常感谢! –