从 SQL 数据库为您的网站创建 RSS feed
如何从 SQL 数据库为您的网站创建一个 RSS 订阅。
引言
前段时间,我考虑开始自己的事业并注册了一个域名等,但后来我得到了一个我无法拒绝的报价,所以我把这些计划搁置了。 我仍然拥有我的域名 (http://www.bid4binary.com) 并且正在支付托管费用,所以我认为我会做一些事情,而博客似乎是自然的选择。 因此,我给自己写了一个博客系统并开始使用它。 不久之后,我的一个读者问我我的 RSS 订阅在哪里,我意识到虽然我听说过 RSS,但我完全不知道它是什么……
背景
因此,我对 RSS 做了一些研究。 RSS 通常被认为是 Real Simple Syndication 的缩写,简而言之,它是一种将内容从您的网站“推送”给选择订阅此内容的用户的方式。 您可以在这里阅读更多关于博客的信息:http://www.bid4binary.com/Item.aspx?iID=72。 我认为这可能很复杂,但令我惊讶的是,从我的博客创建 RSS 订阅非常容易。
使用代码
在您的 ASP.NET 项目中,添加一个名为 *MyFeed.aspx* 的新 Web 窗体。 打开此页面的源代码并删除除第一行之外的所有内容,第一行应如下所示
<%@ Page Language="VB" AutoEventWireup="false"
CodeFile="MyFeed.aspx.vb" Inherits="MyFeed" %>
在此之下,添加以下行
<%@ OutputCache Duration="300" VaryByParam="Category" %>
OutputCache
是 ASP.NET 服务器将页面保留在其缓存中的秒数,以便每次有人打开您的 RSS 订阅时都不会查询数据库。 您可以根据订阅源的更新频率来减少此时间。
可选的 VaryByParam
允许您将参数传递给 SQL 查询,这将更改您的 RSS 订阅的内容。
ASPX 页面就到此为止。 确保保存它,然后让我们继续进行代码隐藏。 打开代码隐藏页面并导入以下命名空间
Imports System.Data.SqlClient
Imports System.Text
Imports System.Xml
现在,将以下内容添加到 Page_Load
事件中
Response.Clear()
Response.ContentType "application/rss+xml;"
Dim xrFeed As XmlTextWriter = New XmlTextWriter
Response.OutputStream, Encoding.UTF8)
xrFeed.WriteStartDocument()
我们需要在所有 RSS 订阅中包含一些元素:title
、link
、ttl
和 description
。
title
- 您的频道名称以及人们将如何引用它。link
- 订阅源来源的网站的 URL。ttl
- “生存时间” - 从源刷新之前可以缓存订阅源的分钟数。description
- 描述 RSS 订阅的短语。
有关这些元素的更多信息,请访问 这里。
xrFeed.WriteStartElement("rss")
xrFeed.WriteAttributeString("version", "2.0")
xrFeed.WriteStartElement("channel")
xrFeed.WriteElementString("title", "Latest Blog Items")
xrFeed.WriteElementString("link", "http://www.mysite.com")
xrFeed.WriteElementString("ttl", "10")
xrFeed.WriteElementString("description", "The latest items from my blog.")
现在我们连接到我们的数据库以检索我们的博客项目以用于我们的订阅源。 我首先从我的“设置”表中选择网站所有者的电子邮件地址,然后从我的博客中选择项目。
Dim clsUtils As New Utils
Dim intBlogID As Integer = clsUtils.intBlogID
Dim strConnection As String = clsUtils.strConnection
Dim connSql As SqlConnection = New SqlConnection(strConnection)
Dim cmdSql As SqlCommand = New SqlCommand
cmdSql.Connection = connSql
cmdSql.CommandText = "SELECT strOwnerEmail From " & _
"tblSiteSettings WHERE intUniqueID = " & intBlogID
connSql.Open()
Dim strOwnerEmail As String = cmdSql.ExecuteScalar
connSql.Close()
cmdSql = New SqlCommand
cmdSql.Connection = connSql
Dim strSql as String = ""
If Trim(Request.QueryString("cID")) <> "" Then
strSql = "SELECT * FROM tblItems WHERE I.intFKBlogItemCategoryID = "
strSql += Request.QueryString("cID") & " AND I.bitActive = 1 "
strSql += "AND I.intFKBlogID = " & intBlogID.ToString & _
" ORDER BY dtPosted DESC"
Else
strSql = "SELECT * FROM tblItems WHERE I.bitActive = 1 AND "
strSql += "I.intFKBlogID = " & intBlogID.ToString & _
" ORDER BY dtPosted DESC"
End If
cmdSql.CommandText = strSql
connSql.Open()
Dim drSql As SqlDataReader = cmdSql.ExecuteReader
Do While drSql.Read
对于我想在订阅源中添加的每个项目,我都创建了几个元素
title
- 此特定项目的标题description
- 此特定项目的内容category
- 此特定项目所属的类别link
- 此特定项目的 URLguid
- 唯一标识此特定项目的字符串pubDate
- 发布此特定项目的日期(注意此格式)
xrFeed.WriteStartElement("item")
xrFeed.WriteElementString("title", drSql("strTitle").ToString)
xrFeed.WriteElementString("description", Replace(drSql("strBody").ToString(), "", vbCrLf))
xrFeed.WriteElementString("category", drSql("strCategory").ToString)
xrFeed.WriteElementString("author", strOwnerEmail & " (" & _
drSql("strPostedBy").ToString & ")")
xrFeed.WriteElementString("link", _
"http://www.mysite.com/Item.aspx?iID=" & _
drSql("intUniqueID"))
xrFeed.WriteElementString("guid", _
"http://www.mysite.com/Item.aspx?iID=" & _
drSql("intUniqueID"))
xrFeed.WriteElementString("pubDate", drSql.GetDateTime(4).ToString("R"))
现在我只需关闭所有内容。
xrFeed.WriteEndElement()
Loop
drSql.Close()
connSql.Close()
xrFeed.WriteEndElement()
xrFeed.WriteEndElement()
xrFeed.WriteEndDocument()
xrFeed.Flush()
xrFeed.Close()
Response.End()
就是这样!
现在,如果您希望您的访问者知道您的网站具有有效的 RSS 订阅,请将以下链接添加到您网站页面的 <head>
部分
<link rel="alternate" type="application/atom+xml"
title="Latest Items" href="http://www.mysite.com/MyFeed.aspx/" />
当他们访问包含此链接的任何页面时,他们的浏览器就会知道您有可用的 RSS 订阅。 此外,如果您的页面包含上述链接,则指向该页面的 RSS 阅读器将自动获取您的订阅源。
关注点
RSS 订阅还具有非常适合搜索引擎的额外好处,并且可能有助于您的网站在搜索结果中排名更高。