In this article I will explain with an example, how to fetch and display RSS Feeds in ASP.Net Core Razor Pages.
This article covers how to fetch RSS Feeds from a URL and display it on the Website in ASP.Net Core Razor Pages.
The RSS Feeds returned from the API
The following RSS Feeds are used in this article.
Model
The Model class consists of following properties which will be used for deserialization of XML data.
[XmlType("rss")]
public class RssModel
{
[XmlElement("channel")]
public Channel Channel { get; set; }
}
[XmlType("channel")]
public class Channel
{
[XmlElement("item")]
public List<Item> Items { get; set; }
}
public class Item
{
[XmlElement("title")]
public string Title { get; set; }
[XmlElement("description")]
public string Description { get; set; }
[XmlElement("link")]
public string Link { get; set; }
[XmlElement("pubDate")]
public string PublishedDate { get; set; }
}
Namepsaces
You will need to import the following namespaces.
using System.Net;
using System.Xml;
using System.Xml.Serialization;
Razor PageModel (Code-Behind)
The PageModel consists of following Handler method.
Handler method for handling GET operation
Inside this Handler method, first the Security Protocol is set.
Then, the RSS Feeds API is called using HttpClient class and the response is received using GetAsync method of HttpResponseMessage class.
If the response is true then, XML string is downloaded using ReadAsStringAsync method of StringReader class.
Then, an object of XmlReader class is created using Create method of XmlReader class.
Finally, the XML string is deserialized into RssModel class using Deserialize method of the XmlSerializer class object and set in the public property.
public class IndexModel : PageModel
{
public RssModel Rss { get; set; }
public void OnGet()
{
//Setting TLS 1.2 protocol.
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
HttpClient client = new HttpClient();
HttpResponseMessage response = client.GetAsync("https://asp-umbraco.azurewebsites.net/rss/content").Result;
if (response.IsSuccessStatusCode)
{
using (StringReader stream = new StringReader(response.Content.ReadAsStringAsync().Result))
{
using (XmlReader reader = XmlReader.Create(stream))
{
XmlSerializer serializer = new XmlSerializer(typeof(RssModel));
this.Rss = (RssModel)serializer.Deserialize(reader);
}
}
}
}
}
Razor Page (HTML)
Inside the Razor Page, in the very first line the RssModel class is declared as model for the View.
The HTML of Razor Page consists of an Anchor tag for displaying the RSS Feeds links.
A FOR EACH loop is executed over the Item class of RssModel and RSS Feeds and HTML Table will be used to display the RSS Feeds contents i.e. Link, Title and Description.
The href property of the Anchor tag is set to Link.
@page
@model Display_RSS_Feeds_Core_Razor.Pages.IndexModel
@using Display_RSS_Feeds_Core_Razor.Models
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
@foreach (Item item in Model.Rss.Channel.Items)
{
<table width="100%">
<tr>
<td><a id="lnkTitle" href="@item.Link">@item.Title</a></td>
</tr>
<tr>
<td>@item.Description</td>
</tr>
</table>
<br />
}
</body>
</html>
Screenshot
Demo
Downloads