Welcome Guest! To enable all features please
Login or Register.
I am still learning the architecture of YAF and am not 100% sure this covers all of the cases, but it seems to be doing what I need.
Any suggestions?
namespace YAF.Modules
{
using System;
using System.Web.UI.HtmlControls;
using YAF.Types;
using YAF.Types.Attributes;
using YAF.Types.Constants;
using YAF.Utils.Helpers;
/// <summary>
/// Generates a canonical meta tag to fight the dreaded duplicate content SEO warning
/// </summary>
[YafModule( "Canonical Meta Tag Module", "BonzoFestoon", 1 )]
public class CanonicalMetaTagModule : SimpleBaseForumModule
{
public override void InitAfterPage()
{
this.CurrentForumPage.PreRender += this.CurrentForumPage_PreRender;
}
/// <summary>
/// Handles the PreRender event of the ForumPage control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
private void CurrentForumPage_PreRender( [NotNull] object sender, [NotNull] EventArgs e )
{
const string topicLinkParams = "t={0}";
HtmlHead head = this.ForumControl.Page.Header
?? this.CurrentForumPage.FindControlRecursiveBothAs<HtmlHead>( "YafHead" );
if ( head != null )
{
// in cases where we are not going to index, but follow, we will not add a canonical tag.
string tag = null;
switch ( this.ForumPageType ) // Just in case there are other types I am not thinking of.
{
case ForumPages.posts:
int topicId = this.PageContext.PageTopicID;
string topicUrl = YAF.Utils.YafBuildLink.GetLink( ForumPages.posts, true, topicLinkParams, topicId );
tag = string.Format( "<link rel=\"canonical\" href=\"{0}\" />", topicUrl );
break;
default:
// there is not much SEO value to having lists indexed
// because they change as soon as some adds a new topic
// or post so don't index them, but follow the links
tag = "<meta name=\"robots\" content=\"noindex,follow\" />";
break;
}
if ( !string.IsNullOrWhiteSpace( tag ) )
{
head.Controls.Add( new System.Web.UI.LiteralControl( tag ) );
}
}
else
{
// Ichabod Crane is running scared!
}
}
}
}
Forum Jump
- You cannot post new topics in this forum.
- You cannot reply to topics in this forum.
- You cannot delete your posts in this forum.
- You cannot edit your posts in this forum.
- You cannot create polls in this forum.
- You cannot vote in polls in this forum.
Important Information:
The YAF.NET Support Forum uses cookies. By continuing to browse this site, you are agreeing to our use of cookies.
More Details
Close