2009-09-09 35 views
7

我正在使用ASP.NET和MasterPages。因此我不能把這個鏈接放在引用我的母版的頁面中。通過後面的代碼將CANONICAL標籤添加到我的SEO頁面?

<link rel="canonical" href="http://www.erate.co.za/" /> 

我需要把這個鏈接放在我的每個頁面的頁面加載中。我如何通過代碼來做到這一點?我使用VB.NET,但C#也將幫助我朝着正確的方向發展。

這就是我爲我的描述標記在我的代碼後面做的。

Dim tag As HtmlMeta = New HtmlMeta() 
    tag.Name = "description" 
    tag.Content = "Find or rate any company in South Africa for FREE and rate them" 
    Header.Controls.Add(tag) 

在此先感謝!

回答

11

這是我不得不做..................

Dim seoTag As HtmlLink = New HtmlLink() 
    seoTag.Attributes.Add("rel", "canonical") 
    seoTag.Href = "http://www.erate.co.za/" 
    Header.Controls.Add(seoTag) 

更多信息Here

2

爲什麼不創建您的規範元素作爲服務器控件:

<link rel="canonical" href="" runat="server" id="canonical"/> 

然後操作規範對象在你的頁面(或母版頁)類。通用標籤都被視爲的HtmlGenericControl情況下,它允許一個任意設置屬性:

canonical.Attributes["href"] = whatever; 
+0

這是我做什麼,我把你的鏈接我的母版標題標籤內。但是,然後從我的正常頁面,你的代碼不起作用。它沒有選擇規範的屬性。 – Etienne 2009-09-09 12:45:32

+0

查看Danrichardson的答案(http://stackoverflow.com/questions/1398821/adding-the-canonical-tag-to-my-page-for-seo-through-code-behind/1399522#1399522)訪問母版頁從頁面進行控制。 – Richard 2009-09-10 12:24:52

0

按照理查德的答案,在你的頁面代碼方面,你將需要引用母版頁。 嘗試:

((HtmlLink)this.Page.Master.FindControl("canonical")).Href = "whatever"; 

或VB相當於:)

0

我有以下設置。

創建一個從System.Web.UI.Page作爲「BasePage」類繼承的類。

添加到一個方法:

public class BasePage: System.Web.UI.Page { 

    // I've tended to create overloads of this that take just an href and type 
    // for example that allows me to use this to add CSS to a page dynamically 
    public void AddHeaderLink(string href, 
          string rel, 
          string type, 
          string media) { 
    HtmlLink link = new HtmlLink(); 
    link.Href = href; 

    // As I'm working with XHTML, I want to ensure all attributes are lowercase 
    // Also, you could replace the length checks with string.IsNullOrEmpty if 
    // you prefer. 
    if (0 != type.Length){ 
     link.Attributes.Add(HtmlTextWriterAttribute.Type.ToString().ToLower(), 
          type); 
    } 

    if (0 != rel.Length){ 
     link.Attributes.Add(HtmlTextWriterAttribute.Rel.ToString().ToLower(), 
          rel); 
    } 

    if (0 != media.Length){ 
     link.Attributes.Add("media", media); 
    } 

    Page.Header.Controls.Add(link); 
    } 
} 

然後,您可以創建一個從基類繼承的.aspx頁,然後調用AddHeaderLink上:

public partial class MyPage : BasePage { 

    protected void Page_Load(object sender, EventArgs e) { 

    // Or however you're generating your canonical urls 
    string cannonicalUrl = GetCannonicalUrl(); 

    AddHeaderLink(cannonicalUrl, "canonical", string.Empty, string.Empty); 
    } 
} 
1

嘗試使用: 首先創建BasePage類是這樣的:

using System; 
using System.Data; 
using System.Configuration; 
using System.Collections; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 
using System.Text.RegularExpressions; 

namespace MMSoftware.TheMMSoft.UI 
{ 
    public class BasePage : System.Web.UI.Page 
    { 
     private string _canonical; 
     // Constructor 
     public BasePage() 
     { 
      Init += new EventHandler(BasePage_Init); 
     } 

     // Whenever a page that uses this base class is initialized 
     // add link canonical if available 
     void BasePage_Init(object sender, EventArgs e) 
     {    
      if (!String.IsNullOrEmpty(Link_Canonical)) 
      { 
       HtmlLink link = new HtmlLink(); 
       link.Href = Link_Canonical; 
       link.Attributes.Add(HtmlTextWriterAttribute.Rel.ToString().ToLower(), "canonical"); 
       link.Attributes.Add(HtmlTextWriterAttribute.Type.ToString().ToLower(), ""); 
       link.Attributes.Add("media", ""); 
       Header.Controls.Add(link); 
      } 
     } 

     /// <summary> 
     /// Gets or sets the Link Canonical tag for the page 
     /// </summary> 
     public string Link_Canonical 
     { 
      get 
      { 
       return _canonical; 
      } 
      set 
      { 
       _canonical = value; 
      } 
     }     
    } 
} 

秒創建一個從基類繼承這樣的.aspx頁:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

public partial class _Default : MMSoftware.TheMMSoft.UI.BasePage 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 
} 

最後一步:

<%@ Page Title="" 
     Language="C#" 
     MasterPageFile="~/design/MasterPage.master" 
     AutoEventWireup="true" 
     CodeFile="Default.aspx.cs" 
     Inherits="_Default" 
     CodeFileBaseClass="MMSoftware.TheMMSoft.UI.BasePage" 
     Link_Canonical="http://yourCanonicalUrl/" 
%> 

請記住在中添加C:\ Program Files \ Microsoft Vi sual Studio 9.0 \ Common7 \ Packages \ schemas \ html \ page_directives。XSD屬性:

<xsd:attribute name="Link_Canonical" vs:nonfilterable="true" /> 

在複雜類型部分

<a href="http://www.dowebpage.com">Michele - MMSoftware </a> 
相關問題