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 -->
我覺得我在想念的東西超級簡單,但我不能爲我的生活解析此引用。幫助表示讚賞。
也做到了,我知道這是愚蠢的東西。非常感謝! –