简体   繁体   中英

Reading RSS Feed with MVC4

I'm trying to read in and process a wordpress blog onto my MVC4 website. I followed this example here but I am getting the following error: Read rss feeds with c# mvc4

Error: Compiler Error Message: CS1061: 'MyWebsites.Models.WordPressRSS' does not contain a definition for 'RSSFeed' and no extension method 'RSSFeed' accepting a first argument of type 'MyWebsites.Models.WordPressRSS' could be found (are you missing a using directive or an assembly reference?)

Model

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

Controller

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



    }
}

View

@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 -->

I feel like im missing something super simple but I cannot for the life of me resolve this reference. Help is appreciated.

I believe the problem is with your view.

In the for each loop, item refers to the WordPressRSS item and not the list.

Try referencing the properties directly.

@item.Title

Instead of

@item.RSSFeed.FirstOrDefault().Title

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM