【问题标题】:Adding meta tag programmatically in C#在 C# 中以编程方式添加元标记
【发布时间】:2016-12-10 02:03:06
【问题描述】:

我正在尝试以编程方式添加<meta>。当.aspx 页面中存在带有runat = "server"Head 元素时,它工作正常。

后面的代码是:

HtmlMeta meta = new HtmlMeta();
meta.Name = "robots";
meta.Content = "noindex,follow";
this.Page.Header.Controls.Add(meta);

但我在 head 标记中有一些脚本,其中包含 <% ... %> 之类的代码块,因此我无法保留 runat = "server" 值。

问题是我必须以编程方式添加元标记,因为它取决于数据库中的值。

有没有办法解决这个问题,以便我在 head 元素中的脚本像往常一样工作,并且我可以通过编程方式添加元标记?

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    好的,我测试了answer by veggerby,效果很好:

    <header> 部分:

    <asp:PlaceHolder id="MetaPlaceHolder" runat="server" />
    

    请注意,Visual Studio 可能会在 PlaceHolder 标记上显示警告,因为它不会被识别为标头内的已知元素,但您可以忽略这一点。它有效。

    在 C# 代码中:

    HtmlMeta meta = new HtmlMeta();
    meta.Name = "robots";
    meta.Content = "noindex,follow";
    MetaPlaceHolder.Controls.Add(meta);
    

    或者(因为您已经在标题部分使用&lt;% %&gt; 的代码块),您可以直接标记元并仅从服务器端检索值:

    <meta name="robots" content="<%=GetMetaRobotsValueFromDatabase()%>" />
    

    【讨论】:

    • 道歉 - 代码太长,无法作为评论发布,所以我在下面添加了一个答案(使用您的代码)。非常感谢!
    【解决方案2】:

    非常感谢Awe for the solution!我在 (error404.ascx) ASP.NET 用户控件中实现了这段代码,如下所示:

    <%@ Control Language="C#"%>
    <script runat="server">
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.TrySkipIisCustomErrors = true;  //Suppress IIS7 custom errors
            Response.StatusCode = 404;
            SetRobotsHeaderMetadata();
        }
    
        private void SetRobotsHeaderMetadata()
        {
            HtmlMeta meta = new HtmlMeta();
            meta.Name = "robots";
            meta.Content = "noindex,follow";
            this.Page.Master.FindControl("cphPageMetaData").Controls.Add(meta);
        }
    </script>
    

    使用以下母版页:

    <%@ Master Language="C#" AutoEventWireup="true" Inherits="MyMaster" %>
    <script runat="server">
        ...
    </script>
    
    <!DOCTYPE html>
    <html lang="en-GB">
        <head>
            <title>Site title here</title>
    
            <asp:contentplaceholder runat="server" id="cphPageMetaData">
            </asp:contentplaceholder>
        </head>
    
        <body>
            ...
        </body>
    </html>
    

    【讨论】:

      【解决方案3】:

      或者你可以把你的元标记放在标题中,带有一个 ID 和一个 runat="server"... 然后在后面的代码中说

      myMetaTag.Content = "noindex,follow";
      

      myMetaTag.Visible = false;
      

      或任何你想要的。

      【讨论】:

        【解决方案4】:

        我认为这是最好的方法:

        this.Page.Header.Controls.Add(new LiteralControl(@"<meta ... />"));
        

        享受吧!

        【讨论】:

        • 我知道这有点晚了,但您有什么理由选择使用LiteralControl 而不是HtmlMeta
        • 此外,我认为这不能回答 OPs 的问题,因为 Header 不是服务器控件。 Page.Header 将为空
        【解决方案5】:

        尝试将您在 中所做的一切移动到代码隐藏中。如果您使用脚本将内容添加到页面中,则可以将其替换为 asp:Literal 控件,然后将您之前在脚本块中计算的值设置为代码隐藏并将 Literal.Text 设置为该值。

        【讨论】:

          【解决方案6】:

          我还没有测试过,但也许你可以在&lt;head&gt;&lt;/head&gt; 标记内添加一个&lt;asp:Placeholder&gt; 并将元标记添加到此。

          【讨论】:

            【解决方案7】:

            最好的解决方案,我成功地检查了,没有任何错误或警告:

            包含 &lt;% ... %&gt; 代码的 JavaScript 代码已从 head 部分中删除并放置在 body 部分中。

            【讨论】:

              【解决方案8】:

              您可以将元标记定义为静态字符串,如下所示:

              Private Shared MetaLanguage As String =
                  String.Format("<meta http-equiv=""Content-Language"" content=""{0}""/>", CultureInfo.CurrentUICulture.TwoLetterISOLanguageName)
              

              然后像这样把它们放在你的头上:

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

              这允许您使用任何元标记值,并且易于阅读和自定义。注意:使用Shared 关键字(静态)有助于提高性能。

              【讨论】:

                【解决方案9】:

                MetaDescription = "你的元描述在这里"; MetaKeywords = "Keyword1,Keyword2,Keyword3";

                【讨论】:

                  【解决方案10】:

                  好的...我实际上只使用 C#... 或将 HTML 转换为 C#。 我从不在 aspx 文件中使用代码隐藏、设计器或 web 控件...所以我从类中编写所有内容...而且是动态的。

                  结果:

                  HtmlMeta meta = new HtmlMeta();
                  meta.Name = "robots";
                  `meta.Content = "Here is what you want";`
                  var page=HttpContext.Current.Handler as Page;
                  page.Header.Controls.Add(meta);
                  

                  【讨论】:

                    猜你喜欢
                    • 2022-08-23
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    • 2014-07-20
                    • 1970-01-01
                    • 2023-01-31
                    • 2015-08-15
                    相关资源
                    最近更新 更多