2009-09-09 25 views
7

Tôi đang sử dụng ASP.NET với MasterPages. Vì vậy, tôi không thể chỉ cần đặt liên kết này trong các trang của tôi tham chiếu MasterPage của tôi.Thêm thẻ CANONICAL vào trang của tôi cho SEO thông qua mã phía sau?

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

Tôi cần đặt liên kết này mặc dù Tải trang của mỗi trang của tôi. Làm thế nào tôi sẽ làm điều này thông qua mã? Tôi đang sử dụng VB.NET nhưng C# cũng sẽ giúp tôi đi đúng hướng.

Đây là cách tôi đã làm điều đó cho thẻ DESCRIPTION của tôi trong mã của tôi phía sau.

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) 

Cảm ơn bạn trước!

Trả lời

11

Đây là những gì tôi phải làm ..................

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

Thông tin thêm Here

2

Tại sao không tạo yếu tố kinh điển của bạn như một điều khiển server:

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

Sau đó thao tác các đối tượng kinh điển trong trang (hoặc trang chủ) lớp học của bạn. Generic thẻ được coi là trường hợp của HtmlGenericControl cho phép một để thiết lập các thuộc tính tùy ý:

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

Đây là những gì tôi đã làm, tôi đặt liên kết của bạn trong thẻ tiêu đề MasterPage của tôi. Nhưng sau đó từ trang bình thường, mã của bạn không hoạt động. Nó không chọn thuộc tính kinh điển. – Etienne

+0

Xem câu trả lời của Danrichardson (http://stackoverflow.com/questions/1398821/adding-the-canonical-tag-to-my-page-for-seo-through-code-behind/1399522#1399522) để truy cập trang cái kiểm soát từ trang. – Richard

0

Như mỗi câu trả lời của Richard, ở bên mã trang của bạn, bạn sẽ cần phải tham khảo trang chủ. Hãy thử:

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

hoặc tương đương VB :)

0

Tôi đã sau đây thiết lập.

Tạo lớp kế thừa từ System.Web.UI.Page làm lớp loại "BasePage".

Thêm một phương pháp để rằng:

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

Sau đó, bạn có thể tạo các trang aspx của bạn mà kế thừa từ lớp cơ sở, và sau đó gọi AddHeaderLink trên rằng:

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

Cố gắng sử dụng: Đầu tiên tạo ra lớp BasePage như thế này:

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

Giây tạo các trang aspx của bạn mà kế thừa từ lớp cơ sở như thế này:

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

    } 
} 

bước cuối:

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

Hãy nhớ thêm vào C: \ Program Files \ Microsoft Vi sual Studio 9.0 \ Common7 \ Packages \ schemas \ html \ page_directives.xsd thuộc tính:

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

trong phần complexType

<a href="http://www.dowebpage.com">Michele - MMSoftware </a> 
Các vấn đề liên quan