修改现有元标记

时间:2011-12-08 18:41:50

标签: c# asp.net

我有这个代码来删除我没有访问权限的现有元标记,因为它在它附带的解决方案DLL中,但基本上,我想删除它附带到我们公司内容的元标记内容。问题是它没有找到元标记,我认为是因为我设置htmlHead = Page.Header的方式;我想我错过了一些东西..但不确定..这段代码在Base类的虚拟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);
            }
        }
    }

我不确定我是否正在为标题提供正确的重新计算,因为它位于Base类中的虚拟Page_Load中。此外,大部分代码都来自此处Code for meta tag removal and replacement

(99%)

非常感谢任何帮助

4 个答案:

答案 0 :(得分:7)

事件发生的顺序可能是一个问题。我在ASP.NET中创建了一个新页面

 <%@ 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);
            //        }
            //    }
            //} 

        }

    }

当我查看源代码时,我看到元标记的内容已被修改。现在,问题可能是在循环时,控件不存在(尚未添加)并且您正在添加它,然后内置代码添加它。

编辑 - 建议将移动代码移至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);
    }

答案 1 :(得分:2)

虽然这是一个旧线程,但我想分享基于LINQ的方法从Page控件集合中选择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");
        }
    }
}

这可以从OnPreRender从用户控件(ascx控件)或Page_PreRender从Page(aspx)调用:

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

希望这有助于某人。

答案 2 :(得分:1)

是的,做到这一点:

HtmlHead pHtml = Page.Header;

代替。创建一个新的HTML标题不会很好地工作;相反,只是分配标题将更好地工作。只需确保现有:

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

存在于页面上,无论是在母版页还是在该页面上。

答案 3 :(得分:0)

如果您要删除编码网站中的元标记,请使用以下代码

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)