RSS 提要无法正确呈现 - 使用 MVC 3
我正在使用 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
(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
提要现在可以正确显示,无需对代码进行任何更改。不知道为什么'
The feed is displaying correctly now without any changes to the code. Dont know why tho'