【问题标题】:Render Dynamic MetaTags in ASP.NET Page in Separate Lines在单独的行中呈现 ASP.NET 页面中的动态元标记
【发布时间】:2013-04-30 13:17:41
【问题描述】:

当我动态添加元标记时,它们会呈现在一行中。

我怎样才能把它们分成单独的一行

我尝试了以下代码,但这对我不起作用

Page.Title = ds.Tables[0].Rows[0]["Title"].ToString() + " by " + ds.Tables[0].Rows[0]["Author"].ToString();
Page.Controls.Add(new LiteralControl("\n"));
Page.MetaDescription = ds.Tables[0].Rows[0]["Desc"].ToString();
Page.Controls.Add(new LiteralControl("\n"));
Page.MetaKeywords = ds.Tables[0].Rows[0]["Keywords"].ToString();
Page.Controls.Add(new LiteralControl("\n"));

我正在为我的网站使用 asp.net 网络表单。感谢您提供这方面的帮助。

【问题讨论】:

  • 我可以通过创建LiteralControl 并使用string builder 将所有元标记附加到LiteralControl 来在单独的行中呈现元标记。如果我找不到任何解决方案,这将是我的第二种方法。 .

标签: asp.net webforms


【解决方案1】:

您尝试添加新行的方式将不起作用,因为 MetaDescription 已准备好存在于特定位置,并且您在“某处”添加新行但不是在之后。

尝试为您的标题一个接一个地添加所有控件:

HtmlMeta metaDes = new HtmlMeta();
metaDes.Name = "description";
metaDes.Content = ds.Tables[0].Rows[0]["Desc"].ToString();

// add the description on the hader.
Page.Header.Controls.Add(metaDes);
// right after add the new line
Page.Header.Controls.Add(new LiteralControl("\n"));

或者你甚至可以简单地在你的标题上添加一个文字,然后只呈现最终输出,一组你的 html 代码。

【讨论】:

    【解决方案2】:

    现在发帖太晚了。但可能对未来的读者有所帮助。

    要在 新行 上显示 keywordsdescription 元数据,请尝试以下操作:

    protected void AddMetaTag()
    {
        HtmlMeta description = new HtmlMeta();
        description.Name = "description";
        description.Content ="some desc";
    
        HtmlMeta keywords = new HtmlMeta();
        keywords.HttpEquiv = "keywords";
        keywords.Name ="site keywords";
    
        this.Page.Header.Controls.Add(new LiteralControl("\r\n"));
        this.Page.Header.Controls.Add(keywords);
        this.Page.Header.Controls.Add(new LiteralControl("\r\n"));
        this.Page.Header.Controls.Add(description);
        this.Page.Header.Controls.Add(new LiteralControl("\r\n"));
    }
    

    输出:

    <meta name="keywords" content="site keywords" />
    <meta name="description" content="some desc />
    

    希望这会有所帮助!

    【讨论】:

      【解决方案3】:

      我发现我可以通过以下方法在元标记中实现新行:

      StringBuilder sb = new StringBuilder();
      meta.Append("<meta name=""description"" content=""");
      meta.Append(description);
      meta.Append("""/>");
      meta.Append(Environment.NewLine());
      
      ....
      
      Page.Header.Controls.Add(new LiteralControl(sb.ToString()));
      

      【讨论】:

        猜你喜欢
        • 2016-08-12
        • 1970-01-01
        • 1970-01-01
        • 2011-07-10
        • 1970-01-01
        • 2016-09-30
        • 1970-01-01
        • 1970-01-01
        • 2014-11-20
        相关资源
        最近更新 更多