Ran*_*ess 2 sql vb.net asp.net rss syndication
这似乎是Scott Mitchell在ASP.NET 3.5中创建联合供稿的一篇很棒的文章.对我来说问题是它使用的是C#和Linq,我目前还不是很敏锐.
http://dotnetslackers.com/articles/aspnet/How-to-create-a-syndication-feed-for-your-website.aspx
有没有人知道System.ServiceModel.Syndication可以使用VB.NET和SQLConnection对象生成类似联合供稿的命名空间的示例可能存在的位置?
我环顾四周,每个例子似乎都是用C#和Linq制作的(这可能证明我需要尽快学习它们而不是以后).
你现在可能已经想到了它,但是这里是完整性和一些VB爱的实现(以及对亡灵巫师徽章的尝试.:)
aspx页面很简单,请注意60秒缓存:
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="YourProject._Default" %>
<%@ OutputCache Duration="60" VaryByParam="Type" %>
Run Code Online (Sandbox Code Playgroud)
您可能想要考虑使用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
Run Code Online (Sandbox Code Playgroud)
最后,为了超级完整,用于创建表的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
Run Code Online (Sandbox Code Playgroud)
完成.VB.NET,SQL连接,System.ServiceModel.Syndication命名空间,没有LINQ.:)
更新:2010年11月11日获得死灵法师徽章.好极了!谢谢!:)
| 归档时间: |
|
| 查看次数: |
2438 次 |
| 最近记录: |