I just love Master Pages - they make my life so much easier.
But every once in a while I find a case where they can be a bit problematic. For example, I'm building a site and just created a page where I don't want search engines/bots to catalog or follow any of the links found in the page once it's rendered.
Instructing the bots is easy, just plunk:
<meta name="robots" content="noindex,nofollow" />
Into the header, and all is fine. But obviously I don't want to put that into my Master Page. I suppose I could figure out a way to put it in the Master Page and then only conditionally 'render' based on some variables that I configure for every page (or via non-default, etc.). There's also the issue of what kind of control/tag you could stuff into the Master Page's header - since ASP.NET 2.0 doesn't really allow you to put these kinds of controls into the header. (Something I touched upon previously...)
So, with that in mind, I chose the approach of just dynamically adding a new literal:
private void SetNoIndexNoFollow() { Literal robot = new Literal(); robot.Text = "<meta name=\"robots\" content=\"noindex,nofollow\" />";this.Page.Header.Controls.AddAt(4, robot);
}
Note that I'm not using Controls.Add(), but Controls.AddAt(). This lets me put the meta tag exactly where I want it.
Comments