我正在使用带有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)
提前致谢!
答案 0 :(得分:13)
这是我必须做的..................
Dim seoTag As HtmlLink = New HtmlLink()
seoTag.Attributes.Add("rel", "canonical")
seoTag.Href = "http://www.erate.co.za/"
Header.Controls.Add(seoTag)
更多信息Here
答案 1 :(得分:3)
为什么不将规范元素创建为服务器控件:
<link rel="canonical" href="" runat="server" id="canonical"/>
然后操作页面(或母版页)类中的规范对象。通用标记被视为HtmlGenericControl
的实例,允许用户设置任意属性:
canonical.Attributes["href"] = whatever;
答案 2 :(得分:1)
根据Richard的回答,在页面代码方面,您需要引用母版页。 尝试:
((HtmlLink)this.Page.Master.FindControl("canonical")).Href = "whatever";
或VB等价物:)
答案 3 :(得分: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;
}
}
}
}
Seconds创建从基类继承的.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" />
<a href="http://www.dowebpage.com">Michele - MMSoftware </a>
答案 4 :(得分: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);
}
}
答案 5 :(得分:0)
这是我所做的: 在名为“ 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>