Right way to work with RSS in ASP.NET Core 1.0 RC2

倾然丶 夕夏残阳落幕 提交于 2019-11-30 04:57:39

According to this issue, System.ServiceModel.Syndication has not yet been ported to ASP.NET Core. Currently, this leaves you with 2 options:

  • Target the full .NET framework to provide access to SyndicationFeed
  • Build your own XML parser using .NET Core to reproduce the functionality you require

Target the full .NET framework

This is undoubtedly the easiest approach depending on your requirements.

If you will be deploying to windows only then you can run ASP.NET Core on top of the .NET 4.X framework. To do this, update your project.json from something like this

frameworks": {
  "netcoreapp1.0": {
    "imports": [
      "dotnet5.6",
      "dnxcore50",
      "portable-net45+win8"
    ]
  }
}

to this:

frameworks": {
  "net452": {
     "frameworkAssemblies": {
         "System.ServiceModel": ""
     }
   }
}

Build your own XML Parser

This will give you the most flexibility, in that you will still be able to run cross platform using the .NET Core framework. It requires a little more work to deserialise the string you have already obtained, but there are lots of examples on how to do just this, e.g. http://www.anotherchris.net/csharp/simplified-csharp-atom-and-rss-feed-parser/

For the sake of completeness, I'll include the final code which is a stripped down version of the sample @Sock linked to in the 'Build your own XML Parser' section of the answer. @Sock's answer is still the most complete answer, but this sample should be useful for anyone looking for a quick, simple code sample for ASP.NET Core.

Model

public class FeedItem
{
    public string Link { get; set; } 
    public string Title { get; set; } 
    public string Content { get; set; } 
    public DateTime PublishDate { get; set; } 
}

Controller

public class ArticlesController : Controller
{
    public async Task<IActionResult> Index()
    {
        var articles = new List<FeedItem>();
        var feedUrl = "https://blogs.msdn.microsoft.com/martinkearn/feed/";
        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri(feedUrl);
            var responseMessage = await client.GetAsync(feedUrl);
            var responseString = await responseMessage.Content.ReadAsStringAsync();

            //extract feed items
            XDocument doc = XDocument.Parse(responseString);
            var feedItems = from item in doc.Root.Descendants().First(i => i.Name.LocalName == "channel").Elements().Where(i => i.Name.LocalName == "item")
                            select new FeedItem
                            {
                                Content = item.Elements().First(i => i.Name.LocalName == "description").Value,
                                Link = item.Elements().First(i => i.Name.LocalName == "link").Value,
                                PublishDate = ParseDate(item.Elements().First(i => i.Name.LocalName == "pubDate").Value),
                                Title = item.Elements().First(i => i.Name.LocalName == "title").Value
                            };
            articles = feedItems.ToList();
        }

        return View(articles);
    }

    private DateTime ParseDate(string date)
    {
        DateTime result;
        if (DateTime.TryParse(date, out result))
            return result;
        else
            return DateTime.MinValue;
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!