Adding Rss Feed To MVC Website in VB.Net

By NoseBagUK on 06/04/2010

I recently wanted to add an rss feed to this website, so set about searching the Internet for a solution and came by this excellent article by Eran Kampf @ DeveloperZen with a really neat solution which i converted to

For a better understanding of the code visit the original article

First up create a new class (RssActionResult) which Inherits ActionResult...

Imports System.ServiceModel.Syndication
Imports System.Xml

    Public Class RssActionResult
        Inherits ActionResult

        Private Property _Feed As SyndicationFeed
        Public Property Feed As SyndicationFeed
                Return _Feed
            End Get
            Set(ByVal value As SyndicationFeed)
                _Feed = value
            End Set
        End Property

        Public Overloads Overrides Sub ExecuteResult(ByVal context As ControllerContext)
            context.HttpContext.Response.ContentType = "application/rss+xml"

            Dim rssFormatter As New Rss20FeedFormatter(Feed)
            Using writer As XmlWriter = XmlWriter.Create(context.HttpContext.Response.Output)
            End Using
        End Sub

    End Class

Then in your controller create a function to return your rss which returns your new "RssActionResult"

    Function RSS(ByVal id As String) As RssActionResult

        'Get Some Data Or Whatever
        Dim feed As New SyndicationFeed("Test Feed", "This is a test feed", New Uri("http://Contoso/testfeed"), "TestFeedID", DateTime.Now)

        Dim items As New List(Of SyndicationItem)()
        Dim item As New SyndicationItem("Test Item", "This is the content for Test Item", New Uri("http://Contoso/ItemOne"), "TestItemID", DateTime.Now)

        feed.Items = items

        Return New RssActionResult With {.Feed = feed}
    End Function

Easy as that. Again all credit to the original source here.

Comments RSS Feed

Add Comment

loading comments..