Using Argotic to create an RSS feed from an ASPX page
It's simple. Thanks Brian, for making such an easy-to-use syndication library in .NET.
You can find Argotic at: http://www.codeplex.com/Argotic
Interesting points about this sample include:
- There's no easy way to get the MIME-type from a certain file extension. (that I know of). I've used a static MIME library to get the MIME type for file extensions. This is included with the example as a referenced library (Okaypublish.Mime.Static)
- The example I've included uses a pre-defined DataTable as its datasource, which you would then wire up to your SQL database.
- I'm using a constant file size for the enclosure file sizes since I'm not packaging any example content files with this source. I've commented out how to get the filesize from the local file. (If you're hosted on a shared/dedicated server, you should probably append Environment.CurrentDirectory to the begining of the file name)
You can download the example from: http://piqd.com/get/q/bor
Source
While I don't know how to post really pretty code samples on the blog, I'll do my best with copy and paste.
// Demo application written by David Smith of OkayPublish LLC
//
// http://okaypublish.com
// http://btsharp.com
// http://ceedien.com
// http://piqd.com
//
using
System;
using
Argotic.Core.Rss;
using
System.Data;
public
partial class feed : System.Web.UI.Page
{
/// <summary>
/// This code will be executed every time somebody visits the RSS feed page
/// </summary>
protected void Page_Load(object sender, EventArgs e)
{
// This will be used to query the correct information from the SQL database
long userId;
// Don't respond if the request is not in the form of:
// "http://example.com/feed.aspx?q=1"
// "http://example.com/feed.aspx?q=2"
if (Request.Params["q"] == null)
{
Response.Output.Write(
"This URL must be in the form of http://example.com/feed.aspx?q=2. Be sure you're not missing the \"q\" parameter.");
return;
}
// Check to see if the GET parameter in "q" is garbage
try
{
userId =
Convert.ToInt64(Request.Params["q"]);
}
catch (ArgumentException)
{
return;
}
catch (OverflowException)
{
return;
}
catch (FormatException)
{
return;
}
if (userId == 0)
{
return;
}
// Create the RSS feed to be published on this query
RssFeed myFeed = new RssFeed();
// Create the new RSS Channel. This has an array of RssItems.
myFeed.Channel =
new RssChannel("Argotic Demo Feed from User #" + userId.ToString(),
"Published feed from Argotic Demo User #" + userId,
new Uri(Request.Url.AbsoluteUri.ToString()));
// Create the data access layer
// NOTIC: this would be your SQL code
ExampleDataLayer exampleDataLayer = new ExampleDataLayer();
// Get all of the items the user has published
using (DataTable dataTable = exampleDataLayer.CreateDataTable(userId))
{
// Loop through the results of the SQL query to the
// database and insert them into the RSS channel items
foreach (DataRow row in dataTable.Rows)
{
// Title of the post
string title = (string)row["Title"];
// Description of the post
string description = (string)row["Description"];
// Time posted
DateTime dateTime = (DateTime)row["Time"];
// Attached file. This will be an RSS enclosure.
string filename = (string)row["File"];
// Get the right MIME-type for the attached file
string fileExtension = Utils.GetExtension(filename);
string mimeType = "";
if (fileExtension.Length > 0)
{
OkayPublish.Mime.Static.
MimeType currentMimeType =
OkayPublish.Mime.Static.
MimeType.FromExtension(fileExtension);
if (currentMimeType != null)
{
mimeType = currentMimeType.MimeName;
}
else
{
mimeType =
"application/octet-stream";
}
}
else
{
mimeType =
"application/octet-stream";
}
// Create the new RSSItem to be inserted into myFeed.Channel
RssItem newItem = new RssItem();
// Bind each of the returned results to a new RSS item
newItem.PublicationDate = dateTime;
newItem.Title = title;
newItem.Description = description;
newItem.Link =
new Uri("http://example.com/readmore.aspx?q=" + filename);
//FileInfo fi = new FileInfo(Environment.CurrentDirectory + @"\" + filename);
newItem.Enclosure =
new RssEnclosure(
new Uri("http://example.com/download.aspx?q=" + filename),
1024 * 30,
//fi.Length, 30 KB, since these aren't real files right now
mimeType
);
// Add the new RSS item to the RSS feed channel (there is only one channel per RSS feed)
myFeed.Channel.Items.Add(newItem);
}
// End Databinding loop
}
// Set the ContentType for the response
Response.ContentType =
"application/xml";
// Write the feed to the output stream
myFeed.Save(Response.OutputStream);
return;
}
}
As always, comments are appreciated. Cheers.