32

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

The code behind is:

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

But I have some script in the head tag which contains code blocks like <% ... %>, so I cannot keep the runat = "server" value.

The problem is I have to add the meta tag programmatically, because it depends on a value from the database.

Is there a way to solve this issue so that my script inside the head element works as usual and I can add a meta tag programmatically?

10 Answers 10

43

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

In the <header> section:

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

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:

<meta name="robots" content="<%=GetMetaRobotsValueFromDatabase()%>" />
Sign up to request clarification or add additional context in comments.

1 Comment

Apologies - The code was too long to post as a comment so I added an answer (using your code) below. Many thanks!
8

Many thanks to Awe for the solution! I have implemented this code in a (error404.ascx) ASP.NET User Control as follows:

<%@ 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>

With the following masterpage:

<%@ 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>

Comments

7

I think this is the best approach:

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

Enjoy!

2 Comments

I know it is a bit late but is there any reason you chose to use a LiteralControl over an HtmlMeta?
Additionally I dont think this answers the OPs question since the Header is not a server control. Page.Header will be null
6

Or you could just put your meta-tag in the header, with an ID and a runat="server"... then in the code behind say

myMetaTag.Content = "noindex,follow";

or

myMetaTag.Visible = false;

or whatever you'd like.

Comments

5

Try moving whatever it is that you are doing in the <% .... %> to the code-behind. If you are using the script to add content into the page, you can replace it with an asp:Literal control and then set the value you were previously calculating in the script block to the code-behind and set Literal.Text to that value.

Comments

2

I haven't tested it, but maybe you can add an <asp:Placeholder> inside the <head></head> tag and add the meta tags to this.

Comments

0

The best solution for this, which I successfully checked without any error or warning:

The JavaScript code, which contains the <% ... %> code, was removed from the head section and placed in the body section.

Comments

0

You could define your meta tag as a static string like so:

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

Then place them in your head like so:

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

This allow you to use any meta tag values and is easy to read and customize. Note: The use of the Shared keyword (static) helps improve performance.

Comments

-3

MetaDescription = "Your meta description goes here"; MetaKeywords = "Keyword1,Keyword2,Keyword3";

Comments

-3

OK... I actually only use C#... Or HTML into C#. I never use codebehind, designer or webcontrols in the file aspx... So I program everything from classes... And dynamically.

Result:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.