添加规范标签到我的网页搜索引擎优化通过代码后面?(Adding the CANONICAL tag

2019-08-04 10:48发布

我使用ASP.NET与MasterPages。 因此,我不能只是放在我的网页引用我的母版此链接。

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

提前致谢!

Answer 1:

这是我必须做的..................

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

更多信息在这里



Answer 2:

为什么不创建您的规范元素作为服务器控件:

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

然后,操作规范对象在你的页面(或母版页)类。 通用标签被视为实例HtmlGenericControl允许一个任意设置属性:

canonical.Attributes["href"] = whatever;


Answer 3:

按照理查德的答案,在你的页面代码方面,你将需要引用母版页。 尝试:

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

或VB相当于:)



Answer 4:

尝试使用:一是创建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文件\微软的Visual Studio 9.0 \ Common7 \包\模式\ HTML \ page_directives.xsd属性:

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

在复杂类型节

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


Answer 5:

我有以下设置。

创建一个从继承的类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);
  }
}


文章来源: Adding the CANONICAL tag to my page for SEO through code behind?