2009-09-09 9 views
7

Sto usando ASP.NET con MasterPages. Quindi non posso semplicemente inserire questo link nelle mie pagine che fanno riferimento alla mia MasterPage.Aggiungendo il tag CANONICAL alla mia pagina per SEO dietro codice?

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

Ho bisogno di inserire questo collegamento nel mio carico di pagina di ciascuna delle mie pagine. Come faccio a farlo attraverso il codice? Sto usando VB.NET ma C# mi aiuterà anche nella giusta direzione.

Ecco come l'ho fatto per il mio tag DESCRIPTION nel mio codice.

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) 

Grazie in anticipo!

risposta

11

questo è quello che aveva a che fare ..................

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

Maggiori informazioni Here

2

perché non creare il vostro elemento canonico come un controllo server:

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

Poi manipolare l'oggetto canonica nella tua classe pagina (o pagina master). tag generici sono trattati come casi di HtmlGenericControl che permette di impostare gli attributi arbitrari:

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

Questo è quello che ho fatto, ho posto il tuo link all'interno del mio tag di intestazione MasterPage. Ma poi dalla mia pagina normale il tuo codice non funziona. Non raccoglie l'attributo canonico. – Etienne

+0

Vedere la risposta di Danrichardson (http://stackoverflow.com/questions/1398821/adding-the-canonical-tag-to-my-page-for-seo-through-code-behind/1399522#1399522) per l'accesso a una pagina master controllo dalla pagina. – Richard

0

Come per la risposta di Richard, nel vostro lato codice della pagina è necessario fare riferimento alla pagina master. Prova:

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

o l'equivalente VB :)

0

Ho il seguente set up.

Creare una classe che eredita da System.Web.UI.Page come classe di tipo "BasePage".

Aggiungere un metodo a che:

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); 
    } 
} 

Quindi è possibile creare le pagine aspx che ereditano dalla classe base, e quindi chiamare AddHeaderLink su quella:

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

Prova d'uso: Innanzitutto creare classe BasePage come questo:

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; 
      } 
     }     
    } 
} 

secondi creano pagine aspx che ereditano dalla classe base come questo:

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) 
    { 

    } 
} 

Ultimo passo:

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

Ricordarsi di aggiungere C: \ Programmi \ Microsoft Vi sual Studio 9.0 \ Common7 \ Packages \ schemas \ html \ page_directives.xsd l'attributo:

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

nella sezione complexType

<a href="http://www.dowebpage.com">Michele - MMSoftware </a> 
Problemi correlati