RSS 提要无法正确呈现 - 使用 MVC 3

发布于 2024-12-11 12:14:00 字数 2883 浏览 0 评论 0原文

我正在使用 MVC 3、VBNET 和 EF 生成 RSS 提要。然而,当我在 IE 中查看 feed url 时,它没有正确呈现 - 我看到的只是一串文本和已编码的 HTML 标签。还有渲染输出的图像

在此处输入图像描述
(来源:riderdesign.net

代码:

 Public Function RSS() As ActionResult

        Dim blogTitle = "RiderDesign Blog"
        Dim blogDescription = "The latest posts from RiderDesign"
        Dim blogUrl = "http://riderdesign.com"
        Dim postUrl = "http://riderdesign.com/Blog/Post/"

        Dim blogItems = _postService.SelectPublishedPosts.ToList()
        Dim postItems = New List(Of SyndicationItem)()
        For Each item In blogItems
            Dim post = New SyndicationItem()
            post.Title = New TextSyndicationContent(item.PostTitle)
            post.Summary = SyndicationContent.CreateHtmlContent(item.PostExcerpt)
            post.LastUpdatedTime = CType(item.PostDateCreated, DateTime)
            post.Content = SyndicationContent.CreateHtmlContent(item.PostText)
            post.AddPermalink(New Uri(postUrl + item.PostId.ToString))
            postItems.Add(post)
        Next

        Dim feed = New SyndicationFeed(blogTitle, blogDescription, New Uri(blogUrl), postItems) With { _
         .Copyright = New TextSyndicationContent("Copyright " & DateTime.Now.Year.ToString() & " RiderDesign.com"), _
         .Language = "en-US" _
        }

        Return New FeedResult(New Rss20FeedFormatter(feed))
        'Return New FeedResult(New Atom10FeedFormatter(feed))
    End Function

    Public Class FeedResult
    Inherits ActionResult
    Public Property ContentEncoding() As Encoding

    Public Property ContentType() As String

    Private ReadOnly m_feed As SyndicationFeedFormatter
    Public ReadOnly Property Feed() As SyndicationFeedFormatter
        Get
            Return m_feed
        End Get
    End Property

    Public Sub New(ByVal feed As SyndicationFeedFormatter)
        Me.m_feed = feed
    End Sub

    Public Overrides Sub ExecuteResult(ByVal context As ControllerContext)
        If context Is Nothing Then
            Throw New ArgumentNullException("context")
        End If

        Dim response As HttpResponseBase = context.HttpContext.Response
        response.ContentType = If(Not String.IsNullOrEmpty(ContentType), ContentType, "application/rss+xml")

        If ContentEncoding IsNot Nothing Then
            response.ContentEncoding = ContentEncoding
        End If

        If m_feed IsNot Nothing Then
            Using xmlWriter = New XmlTextWriter(response.Output)
                xmlWriter.Formatting = Formatting.Indented
                m_feed.WriteTo(xmlWriter)
            End Using
        End If
    End Sub
End Class

I am generating an RSS feed using MVC 3, VBNET and EF. However when i view the feed url in IE, its not being rendered properly - all i see is a string of text and HTML tags have been encoded. Also the image for what the rendered output looks like

enter image description here
(source: riderdesign.net)

Code:

 Public Function RSS() As ActionResult

        Dim blogTitle = "RiderDesign Blog"
        Dim blogDescription = "The latest posts from RiderDesign"
        Dim blogUrl = "http://riderdesign.com"
        Dim postUrl = "http://riderdesign.com/Blog/Post/"

        Dim blogItems = _postService.SelectPublishedPosts.ToList()
        Dim postItems = New List(Of SyndicationItem)()
        For Each item In blogItems
            Dim post = New SyndicationItem()
            post.Title = New TextSyndicationContent(item.PostTitle)
            post.Summary = SyndicationContent.CreateHtmlContent(item.PostExcerpt)
            post.LastUpdatedTime = CType(item.PostDateCreated, DateTime)
            post.Content = SyndicationContent.CreateHtmlContent(item.PostText)
            post.AddPermalink(New Uri(postUrl + item.PostId.ToString))
            postItems.Add(post)
        Next

        Dim feed = New SyndicationFeed(blogTitle, blogDescription, New Uri(blogUrl), postItems) With { _
         .Copyright = New TextSyndicationContent("Copyright " & DateTime.Now.Year.ToString() & " RiderDesign.com"), _
         .Language = "en-US" _
        }

        Return New FeedResult(New Rss20FeedFormatter(feed))
        'Return New FeedResult(New Atom10FeedFormatter(feed))
    End Function

    Public Class FeedResult
    Inherits ActionResult
    Public Property ContentEncoding() As Encoding

    Public Property ContentType() As String

    Private ReadOnly m_feed As SyndicationFeedFormatter
    Public ReadOnly Property Feed() As SyndicationFeedFormatter
        Get
            Return m_feed
        End Get
    End Property

    Public Sub New(ByVal feed As SyndicationFeedFormatter)
        Me.m_feed = feed
    End Sub

    Public Overrides Sub ExecuteResult(ByVal context As ControllerContext)
        If context Is Nothing Then
            Throw New ArgumentNullException("context")
        End If

        Dim response As HttpResponseBase = context.HttpContext.Response
        response.ContentType = If(Not String.IsNullOrEmpty(ContentType), ContentType, "application/rss+xml")

        If ContentEncoding IsNot Nothing Then
            response.ContentEncoding = ContentEncoding
        End If

        If m_feed IsNot Nothing Then
            Using xmlWriter = New XmlTextWriter(response.Output)
                xmlWriter.Formatting = Formatting.Indented
                m_feed.WriteTo(xmlWriter)
            End Using
        End If
    End Sub
End Class

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回梦 2024-12-18 12:14:00

提要现在可以正确显示,无需对代码进行任何更改。不知道为什么'

The feed is displaying correctly now without any changes to the code. Dont know why tho'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文