Adding meta tag programmatically in C#

后端 未结 10 1170
渐次进展
渐次进展 2020-12-14 01:24

I\'m trying to programmatically add a . It is working fine when there is a Head element with runat = \"server\" in the

10条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-14 01:55

    OK, I tested the answer by veggerby, and it works perfectly:

    In the

    section:

    
    

    Note that Visual Studio might show a warning on the PlaceHolder tag, because it is not recognised as a known element inside the header, but you can ignore this. It works.

    In the C# code:

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

    Alternatively (since you already have code blocks using <% %> in your header section), you can tag the meta directly and retrieve only the value from server side:

    
    

提交回复
热议问题