【问题标题】:Adding the CANONICAL tag to my page for SEO through code behind?通过后面的代码将 CANONICAL 标记添加到我的页面以进行 SEO?
【发布时间】:2010-11-26 19:06:35
【问题描述】:

我正在使用带有 MasterPages 的 ASP.NET。因此,我不能只将此链接放在引用我的 MasterPage 的页面中。

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

我需要将此链接放在我的每个页面的页面加载中。我将如何通过代码做到这一点?我正在使用 VB.NET,但 C# 也会帮助我朝着正确的方向前进。

这就是我在后面的代码中为我的DESCRIPTION标签做的。

    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# asp.net vb.net seo


    【解决方案1】:

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

        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;
      

      【讨论】:

      • 这就是我所做的,我将您的链接放在我的 MasterPage 标题标签中。但是从我的正常页面中,您的代码不起作用。它没有拾取规范属性。
      • 查看 Danrichardson 的回答 (stackoverflow.com/questions/1398821/…) 以从页面访问母版页控件。
      【解决方案3】:

      根据 Richard 的回答,在您的页面代码方面,您需要引用母版页。 试试:

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

      或等效的 VB :)

      【讨论】:

        【解决方案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\Microsoft Visual Studio 9.0\Common7\Packages\schemas\html\page_directives.xsd中添加属性:

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

        在 complexType 部分

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

        【讨论】:

          【解决方案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);
            }
          }
          

          【讨论】:

            【解决方案6】:

            这是我所做的: 在名为“Masterpage.master”头标签的母版页中,我添加了一个 contentPlaceHolder,如下所示:

            <asp:ContentPlaceHolder ID="forcanonical" runat="server">
            </asp:ContentPlaceHolder>
            

            然后在每个子aspx页面页面(不是后面的代码)中,我添加了以下内容:

            <asp:Content ID="Content1" runat="server" ContentPlaceHolderID="forcanonical">
                <link rel="canonical" href="http://theCanonicalUrl.com" />
            </asp:Content>
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2017-03-29
              • 2012-01-04
              • 2020-06-13
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多