おそらくもうお分かりだと思いますが、これは完全性と VB への愛情を込めた実装です (そして、ネクロマンサー バッジの試みです. :)
aspx ページは単純です。60 秒のキャッシュに注意してください。
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="YourProject._Default" %>
<%@ OutputCache Duration="60" VaryByParam="Type" %>
代わりに HttpHandler を使用することを検討することをお勧めしますが、これも問題なく機能します。
背後にあるコード:
Imports System.ServiceModel.Syndication
Imports System.Xml
Partial Public Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim dbConn As String = "[your db connection]"
Dim format As FeedFormats = GetFeedFormat()
Dim posts As New List(Of SyndicationItem)
Using cnn As New SqlClient.SqlConnection(dbConn)
cnn.Open()
Using cmd As New SqlClient.SqlCommand("SELECT ID, Title, Text, Url, Created FROM Posts", cnn)
Dim reader As SqlClient.SqlDataReader = cmd.ExecuteReader
While reader.Read
Dim item As New SyndicationItem(reader.Item("Title"), reader("Text"), New Uri(reader("Url")))
posts.Add(item)
End While
End Using
End Using
Dim feed As New SyndicationFeed("Your feed title", "Your feed description", New Uri("http://yourdomain.com"), posts)
Using feedWriter As XmlWriter = XmlWriter.Create(Response.OutputStream)
Select Case format
Case FeedFormats.Atom
Response.ContentType = "application/rss+xml"
Dim atomFormatter As New Atom10FeedFormatter(feed)
atomFormatter.WriteTo(feedWriter)
Case FeedFormats.Rss
Response.ContentType = "application/atom+xml"
Dim rssFormatter As New Rss20FeedFormatter(feed)
rssFormatter.WriteTo(feedWriter)
End Select
End Using
End Sub
Private Function GetFeedFormat() As FeedFormats
If Request.QueryString("format") = "atom" Then
Return FeedFormats.Atom
Else
Return FeedFormats.Rss
End If
End Function
Public Enum FeedFormats
Rss = 1
Atom = 2
End Enum
End Class
最後に、超完全を期すために、テーブルを作成するための SQL スクリプト:
CREATE TABLE [dbo].[Posts](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Title] [nvarchar](50) NOT NULL,
[Text] [ntext] NOT NULL,
[Url] [nvarchar](50) NOT NULL,
[Created] [datetime2](7) NOT NULL,
CONSTRAINT [PK_Posts] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
ALTER TABLE [dbo].[Posts] ADD CONSTRAINT [DF_Posts_Url] DEFAULT ('') FOR [Url]
GO
ALTER TABLE [dbo].[Posts] ADD CONSTRAINT [DF_Posts_Created] DEFAULT (getdate()) FOR [Created]
GO
終わり。VB.NET、SQL 接続、System.ServiceModel.Syndication
名前空間、LINQ なし。:)
更新: 2010 年 11 月 11 日にネクロマンサー バッジを取得しました。わーい!ありがとう!:)