2

我在 ASP.NET MVC 中看到了一些 RSS 源的示例,例如this,以及项目中的一些示例(例如 Oxite),但它们都不完整。

例如。他们都没有检查标题

  If-Modified-Since

在请求中,以节省带宽。

我不想重新发明轮子,所以我在这里停下来询问一些方向。

4

3 回答 3

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>
  <% } %>
于 2008-12-15T23:03:49.613 回答
2

我结束了这个。如果您发现任何错误或更好的方法,请评论或编辑帖子。

Rss控制器

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

Rss 视图 (rss.aspx)

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()

这样,您可以重用视图

于 2008-12-16T17:16:33.463 回答
2

如果您使用HttpContext.Response.Cache.SetCacheability,SetExpiresSetMaxAgeASP.NET 缓存系统将为您处理此问题 - 它理解 If-Modified-Since 标头。此博客条目展示了如何创建 MVC 操作过滤器以在 MVC 中执行必要的操作。

于 2008-12-18T11:39:03.817 回答