良好的全面实施的RSS在ASP.net MVC供稿 [英] Good and full implementation of RSS feeds in ASP.net MVC

查看:116
本文介绍了良好的全面实施的RSS在ASP.net MVC供稿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过RSS的几个例子供稿在ASP.NET MVC中,像这个,并在项目中(如Oxite)一些样品,但他们都不是完整的。

I've seen a few examples of RSS Feeds in ASP.NET MVC, like this, and some samples in projects (like Oxite), but none of them are complete.

例如。他们没有检查标题

Eg. None of them check for the header

  If-Modified-Since

在请求中,以节省带宽。

in the request, to save bandwidth.

我不希望推倒重来,所以我停在这里询问的一些方向。

I do not want to reinvent the wheel, so I stop here asking for some directions.

推荐答案

我结束了这一点。请评论或编辑后,如果您发现任何错误或更好的方法来做到这一点。

I ended up with this. Please comment or edit the post if you find any error or better way to do it.

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

这样,您就可以重复使用视图

This way, you can reuse the view

这篇关于良好的全面实施的RSS在ASP.net MVC供稿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆