修改现有的元标记

本文关键字:修改 | 更新日期: 2023-09-27 18:17:28

我有这个代码来删除一个现有的元标签,我没有访问权限,因为它是在它带来的解决方案dll中,但基本上,我想删除它带来的元标签内容到我们的公司内容。问题是它没有找到元标签,我认为是因为我设置htmlHead = Page.Header的方式;我想我遗漏了一些东西。但不确定…此代码位于基类中的虚拟Page_Load中。

    HtmlHead pHtml = Page.Header;
    for (int i = pHtml.Controls.Count - 1; i >= 0; i--)
    {
        if (pHtml.Controls[i] is HtmlMeta)
        {
            pMeta thisMetaTag = (HtmlMeta)pHtml.Controls[i];
            if (thisMetaTag.Name == mName)
            {
                pHtml.Controls.RemoveAt(i);
            }
        }
    }

我不确定我是否给出了正确的引用头,因为这是在一个基类的虚拟Page_Load中。此外,大部分代码取自(99%)这里的元标签删除和替换代码

如有任何帮助,不胜感激

修改现有的元标记

可能是事件发生的顺序有问题。我用ASP创建了一个新页面。净

 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="FormMail.WebForm1" %>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml" >
 <head runat="server">
     <title>Untitled Page</title>
     <meta http-equiv="keyword" name="testy" content="default content" />
 </head>
 <body>
     <form id="form1" runat="server">
     <div>
     </div>
     </form>
 </body>
 </html>

然后使用:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string mName = "testy";
            HtmlHead pHtml = Page.Header;
            foreach (HtmlMeta metaTag in pHtml.Controls.OfType<HtmlMeta>())
            {
                if (metaTag.Name.Equals(mName, StringComparison.CurrentCultureIgnoreCase))
                {
                    metaTag.Content = "Yeah!";
                    break; //You could keep looping to find other controls with the same name, but I'm exiting the loop
                }
            }

            //for (int i = pHtml.Controls.Count - 1; i >= 0; i--)
            //{
            //    if (pHtml.Controls[i] is HtmlMeta)
            //    {
            //        HtmlMeta thisMetaTag = (HtmlMeta)pHtml.Controls[i];
            //        if (thisMetaTag.Name == mName)
            //        {
            //            thisMetaTag.Content = "Yeah!";
            //           // pHtml.Controls.RemoveAt(i);
            //        }
            //    }
            //} 
        }
    }

查看源代码时,我看到元标记的内容被修改了。现在,你的问题可能是在循环的时候,控件不存在(还没有添加),你正在添加它,然后内建的代码正在添加它。

EDIT -建议将代码移动到PreRender,以防控件在加载后但在渲染之前添加

    protected override void OnPreRender(EventArgs e)
    {
        if (!IsPostBack)
        {
            string mName = "testy";
            HtmlHead pHtml = Page.Header;
            foreach (HtmlMeta metaTag in pHtml.Controls.OfType<HtmlMeta>())
            {
                if (metaTag.Name.Equals(mName, StringComparison.CurrentCultureIgnoreCase))
                {
                    metaTag.Content = "Yeah!";
                    break;
                }
            }
        }
        base.OnPreRender(e);
    }

虽然这是一个旧的线程,我想分享一个基于LINQ的方法来从页面控件集合中选择HtmlMeta控件:

HtmlMeta htmlMetaCtrl = (from ctrls in page.Header.Controls.OfType<HtmlMeta>()
                                     where ctrls.Name.Equals("keywords", StringComparison.CurrentCultureIgnoreCase)
                                     select ctrls).FirstOrDefault();
            if (htmlMetaCtrl != null)
                htmlMetaCtrl.Content = metaContent;

以下通用函数可用于动态更改元标记:

    public class WebUtils
{
    public static void SetPageMeta(string metaName, string metaContent, HttpContext httpContext = null)
    {
        if (string.IsNullOrWhiteSpace(metaName))
            return;
        if (metaContent == null)
            throw new Exception("Dynamic Meta tag content can not be null. Pl pass a valid meta tag content");
        if (httpContext == null)
            httpContext = HttpContext.Current;
        Page page = httpContext.Handler as Page;
        if (page != null)
        {
            HtmlMeta htmlMetaCtrl = (from ctrls in page.Header.Controls.OfType<HtmlMeta>()
                                     where ctrls.Name.Equals(metaName, StringComparison.CurrentCultureIgnoreCase)
                                     select ctrls).FirstOrDefault();
            if (htmlMetaCtrl != null)
                page.Header.Controls.Remove(htmlMetaCtrl);
            htmlMetaCtrl = new HtmlMeta();
            htmlMetaCtrl.HttpEquiv = metaName;
            htmlMetaCtrl.Name = metaName;
            htmlMetaCtrl.Content = metaContent;
            page.Header.Controls.Add(htmlMetaCtrl);
        }
        else
        {
            throw new Exception("Web page handler context could not be obtained");
        }
    }
}

这可以从用户控件(ascx控件)的OnPreRender中调用,也可以从页面(aspx)的Page_PreRender中调用:

        protected override void OnPreRender(EventArgs e)
    {
        if (!IsPostBack)
        {
    WebUtils.SetPageMeta("keywords", "Your keyword, keyword2, keyword3");
    WebUtils.SetPageMeta("description", "Your page description goes here...");
        }
        base.OnPreRender(e);
    }

是的,这样做:

HtmlHead pHtml = Page.Header;

。创建一个新的HTML头并不能很好地工作;相反,仅仅分配标题将工作得更好。只要确保存在:

<head runat="server"></head>

存在于页面中,要么在母版页中,要么在该页中。

如果您想在编码站点中删除元标记,则使用以下代码

Dim meta4 As New HtmlMeta()
    meta4.Name = "AUTHOR"
    meta4.Content = "AUTHOR"
    Me.Page.Header.Controls.Remove(meta4)

    Dim meta5 As New HtmlMeta()
    meta5.Name = "DESCRIPTION"
    meta5.Content = "DESCRIPTION"
    Me.Page.Header.Controls.Remove(meta5)

    Dim meta6 As New HtmlMeta()
    meta6.Name = "KEYWORDS"
    meta6.Content = "KEYWORDS"
    Me.Page.Header.Controls.Remove(meta6)