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.
Note: For more details on ASP.Net Core Razor Pages, please refer my article ASP.Net Core Razor Pages: Hello World Tutorial with Sample Program example.
 
 

The RSS Feeds returned from the API

The following RSS Feeds are used in this article.
ASP.Net Core Razor Pages: Fetch and Display RSS Feeds
 
 

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 { getset; }
}
 
[XmlType("channel")]
public class Channel
{
    [XmlElement("item")]
    public List<Item> Items { getset; }
}
 
public class Item
{
    [XmlElement("title")]
    public string Title { getset; }
 
    [XmlElement("description")]
    public string Description { getset; }
 
    [XmlElement("link")]
    public string Link { getset; }
 
    [XmlElement("pubDate")]
    public string PublishedDate { getset; }
}
 
 

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.
Note: For more information on using TLS 1.2 with HttpClient, please refer my article Using TLS1.2 with HttpClient in C# and VB.Net and for previous .Net Framework versions, please refer Using TLS1.2 in .Net 2.0, .Net 3.0, .Net 3.5 and .Net 4.0.
 
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

ASP.Net Core Razor Pages: Fetch and Display RSS Feeds
 
 

Demo

 
 

Downloads