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" />

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!