我在ASP.NET MVC中看到了一些RSS源,例如this,以及项目中的一些示例(如Oxite),但没有一个是完整的。
EG。他们都没有检查标题
If-Modified-Since
在请求中,以节省带宽。
我不想重新发明轮子,所以我在此停下来询问一些方向。
答案 0 :(得分:4)
我还没有看到它实现HTTP_IF_MODIFIED_SINCE,但我会研究使用SyndicationFeed类。它使得在没有任何解析的情况下处理提要非常简单。我目前正在将它用于Atom提要,但它也适用于RSS:
function SyndicationFeed GetFeed(string url) {
XmlReader reader = XmlReader.Create(url);
SyndicationFeed feed = SyndicationFeed.Load(reader);
return feed;
}
public ActionResult ShowFeed()
{
string feedUrl = "somefeedurl";
SyndicationFeed feed = GetFeed(feedUrl);
return View(feed);
}
...然后在视图中:
<%foreach (var item in ViewData.Model.Items) { %>
<li><a href="<%=item.Id %>"><%=item.Title.Text %></a></li>
<% } %>
答案 1 :(得分:2)
我最终得到了这个。如果您发现任何错误或更好的方法,请评论或编辑帖子。
Imports System.ServiceModel.Syndication
Imports System.Xml
Imports System.Web.HttpContext
Function MasterRSS()
Dim baseURL As String = "http://www.mysite.com"
Dim feed As New SyndicationFeed("MySite - Master RSS", "", New Uri(baseURL))
''//Add a custom attribute.
Dim xqName As New XmlQualifiedName("CustomAttribute")
feed.AttributeExtensions.Add(xqName, "Value")
Dim sp As New SyndicationPerson("jerry@mysite.com", "Jerry Seinfeld", "http://Jerry.blog.com")
feed.Authors.Add(sp)
Dim category As New SyndicationCategory("Category")
feed.Categories.Add(category)
feed.Contributors.Add(New SyndicationPerson("cosmo@mysite.com", "Cosmo Kramer", "http://kramer.blog.com"))
feed.Copyright = New TextSyndicationContent("MySite 2008")
feed.Description = New TextSyndicationContent("My description")
''//Add a custom element.
Dim doc As New XmlDocument()
Dim feedElement As XmlElement = doc.CreateElement("CustomElement")
feedElement.InnerText = "Some text"
feed.ElementExtensions.Add(feedElement)
feed.Generator = "Custom"
feed.Id = "MySiteFeedID"
feed.ImageUrl = New Uri("http://www.mysite.com/content/images/logo.png")
''//Items
Dim ModifiedSince As Date
If Not Date.TryParse(Current.Request.Headers("If-Modified-Since"), ModifiedSince) Then
ModifiedSince = Date.Today.AddDays(-30) ''//Or whatever make sense to you.
Else
ModifiedSince.AddMinutes(-5) ''//Just in case, we do not want to miss any item.
End If
''//the list of items.
Dim items As New List(Of SyndicationItem)()
Dim db As New mainDataContext
Dim textContent As TextSyndicationContent, Item As SyndicationItem
''//Then send the list of post, comments, whatever.
Dim Posts = (From p In db.Posts Where c.Date >= ModifiedSince Order By p.Date Descending)
For Each Post In Posts
Dim sb As New StringBuilder
sb.AppendFormat("<p>{0}</p>", Post.FullText)
textContent = New TextSyndicationContent(sb.ToString, TextSyndicationContentKind.Html)
Item = New SyndicationItem("Post " + Post.PostID.ToString, textContent, New Uri(baseURL + "/Post/View/" + Post.PostID.ToString), "Post" + Post.PostID.ToString, Post.Date)
items.Add(Item)
Next
If items.Count = 0 Then
''//send a 304 to the browser.
Return View("304")
End If
feed.Items = items
feed.Language = "es-ar"
feed.LastUpdatedTime = (From i In items Select i.LastUpdatedTime Order By LastUpdatedTime Descending).FirstOrDefault
''//Not needed in this sample.
''//Dim link As New SyndicationLink(New Uri("http://server/link"), "alternate", "Link Title", "text/html", 1000)
''//feed.Links.Add(link)
ViewData("feed") = feed
Return View("Rss")
End Function
Dim htmlwriter As System.IO.Stream = Response.OutputStream
Dim rssWriter As System.Xml.XmlWriter = System.Xml.XmlWriter.Create(htmlwriter)
Dim feed As System.ServiceModel.Syndication.SyndicationFeed = ViewData("feed")
Dim rssFormatter As New System.ServiceModel.Syndication.Rss20FeedFormatter(feed)
rssFormatter.WriteTo(rssWriter)
rssWriter.Close()
这样,您可以重复使用视图
答案 2 :(得分:2)
如果您使用HttpContext.Response.Cache.SetCacheability
,SetExpires
和SetMaxAge
,那么ASP.NET缓存系统将为您解决此问题 - 它了解If-Modified-Since标头。 This blog entry显示了如何创建MVC Action Filter以在MVC中执行必要的操作。