Add Dynamic link rel=alternate RSS directive to ASP.NET Page

"There are times when the best deals are those that aren't done"
-- David Garrity  (re:MSFT-->YHOO)

I have some pages on my "fun" site, ittyurl.net, that generate and display blog search results and offer a custom RSS feed of same. The url to the page that generates the RSS feed must be dynamic, with a search term on the querystring.  But how can you "turn on" the Feed icon in Internet Explorer so it will show that there is a feed available for the custom results page, and which will correctly point to the generated RSS page?

It's pretty simple:

First, let's put the <link .. tag into the HEAD of the ASP.NET ASPX page, and mark it runat="server":

<head runat="server">
    <title>Untitled Page</title>
    <link id="rssLink" rel="Alternate" type="application/rss+xml" title="RSS" href="" runat="server" />
</head>

Next, after we are done with our custom assembly of the feed (I do it from a DataSet that comes from a SQL query here), this is all we need to do:

protected void Page_Load(object sender, EventArgs e)
{
    //Do some custom assembly of your feed based on "t" querystring
    // search term here
    string rssSearch = Request["t"];
    this.rssLink.Attributes["href"] = "rss2.aspx?t=" + rssSearch; 
  
// now the RSS icon in IE is lit up!

}

 

What if my <link.. tag is in my MasterPage?

 

Not a problem. Here is sample code, similar to the above:

 

HtmlLink rssLink = (HtmlLink)Page.Master.FindControl("rssLink");
rssLink.Attributes["href"] = "RSS2.aspx?t=" + txtSearch.Text;

That's all there is to it!

Comments

  1. Anonymous3:04 PM

    Thanks! I had the exact same problem, wanted to create a dymanic RSS-URL and add it to the link-tag. But I couldn't get < % = to work inside the link-tag. And it's really hard to google on < % = i found out (cant post it right here either), but found you page at last.

    ReplyDelete
  2. Hmmm. My feed is not dynamic, so I put this in my master page, but it doesn't seem to cause the autodiscovery:

    < link rel="alternate" type="application/rss+xml" title="RSS Feed for my website" href="http://my.website/feed.xml" / >

    Can I do that in a master page, or do I need to do something else?

    ReplyDelete

Post a Comment

Popular posts from this blog

Some observations on Script Callbacks, "AJAX", "ATLAS" "AHAB" and where it's all going.

IE7 - Vista: "Internet Explorer has stopped Working"

FIREFOX / IE Word-Wrap, Word-Break, TABLES FIX

System.Web.Caching.Cache, HttpRuntime.Cache, and IIS Recycles

FIX: Requested Registry Access is not allowed (Visual Studio 2008)