subha
subha

Reputation: 677

Adding meta tag programmatically in C#

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?

Upvotes: 32

Views: 71488

Answers (10)

user3425953
user3425953

Reputation: 1

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

Upvotes: -3

George Filippakos
George Filippakos

Reputation: 16569

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.

Upvotes: 0

hemantrautela
hemantrautela

Reputation: 84

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.

Upvotes: 0

Jonathan Williams
Jonathan Williams

Reputation: 2055

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>

Upvotes: 8

veggerby
veggerby

Reputation: 9020

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.

Upvotes: 2

awe
awe

Reputation: 22442

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()%>" />

Upvotes: 43

SmartDev
SmartDev

Reputation: 2862

I think this is the best approach:

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

Enjoy!

Upvotes: 6

Nathan
Nathan

Reputation: 7

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

Upvotes: -3

Timothy Khouri
Timothy Khouri

Reputation: 31845

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.

Upvotes: 6

Mike
Mike

Reputation: 643

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.

Upvotes: 5

Related Questions