25

I have several content pages hanging off of one master page. I need to add a refresh meta tag to one of the content pages but I can't see where I can do this.

Any help would be much appreciated.

balexandre
  • 73,608
  • 45
  • 233
  • 342
doogdeb
  • 403
  • 1
  • 4
  • 15

6 Answers6

21

Have not tried this with refresh, but in general you can add a meta tag like this:

   var keywords = new HtmlMeta { Name = "keywords", Content = "one,two,three" };
                Header.Controls.Add(keywords);

update: it is possible this way. Check Rick Strahl http://www.west-wind.com/weblog/posts/2006/Aug/04/No-more-Meta-Refresh-Tags

Pleun
  • 8,856
  • 2
  • 30
  • 50
  • 1
    I tried doing the following as above : if( !IsPostBack ) { //Meta Tags. var metaTags = new HtmlMeta { Name = "W", Content = "Smart"}; Header.Controls.Add(metaTags); metaTags = new HtmlMeta { Name = "W.cg", Content = "afasfsa" }; Header.Controls.Add(metaTags); Gives me an error : Cannot modify controls collection. Your help would be appreciated. – Ruruboy Dec 17 '13 at 19:19
17

This page explains the new feature: ASP.Net 4 adds 2 new Meta tag related properties to the Page. They can be used to set meta tags for keywords and description.

You can set them in the code behind:

Page.MetaKeywords = "keyword1, keyword2, keyword3";
Page.MetaDescription = "Example of new meta tag support in ASP.Net 4";

You can also set in the @Page directive:

<%@ Page Language="C#" AutoEventWireup="true"
MetaKeywords="keyword1, keyword2, keyword3"
MetaDescription="Example of new meta tag support in ASP.Net 4"
CodeFile="Default.aspx.cs" Inherits="_Default" %>

The ouput of either of these methods renders html similar to the following:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>
        ASP.NET 4 Meta Tag Support
    </title>
    <meta name="description" content="Example of new meta tag support in ASP.Net 4" />
    <meta name="keywords" content="keyword1, keyword2, keyword3" />
</head>
<body>
</body>
</html>
John Weisz
  • 30,137
  • 13
  • 89
  • 132
DOK
  • 32,337
  • 7
  • 60
  • 92
5
protected void Page_Load(object sender, EventArgs e)
{
  Page.Title          = "Title of page";
  HtmlMeta tag        = new HtmlMeta();
  tag.Name            = "description";
  tag.Content         = "description of page";
  Header.Controls.Add(tag);
  HtmlMeta tagKeyword = new HtmlMeta();
  tagKeyword.Name     = "keywords";
  tagKeyword.Content  = "keywords of page";
  Header.Controls.Add(tagKeyword );
}

(source url)

j0k
  • 22,600
  • 28
  • 79
  • 90
Rohit
  • 61
  • 1
  • 6
4

add below code in designer page

    <meta id="metaDescription" runat="server" name="Description" />

Now add below code to your .cs page

    Page.MetaKeywords = "keyword1, keyword2, keyword3";
    Page.MetaDescription = "Example of new meta tag";
Kisan Patel
  • 101
  • 3
4

You can add a content place holder on the master page in the head section of the html. You can then add stuff to this content section in your specific content page and it will be outputted to the page header.

Richard Forrest
  • 3,567
  • 2
  • 23
  • 32
2

One way I found to do this (that I didn't see listed here) was to have a Literal and fill it with whatever kinds of meta tags you want. In my case, I needed to use it without a Master page, to have Facebook recognize a thumbnail image, title, and description:

<head runat="server">
    <asp:Literal runat="server" ID="litMeta" />
...
</head>

CodeBehind:

var img = "<meta property=\"og:image\" content=\"thumbnail.jpg\" />";
var title = "<meta property=\"og:title\" content=\"Title\" />";
var desc = "<meta property=\"og:description\" content=\"Description\" />";
litMeta.Text = img + title + desc;
DLeh
  • 23,806
  • 16
  • 84
  • 128
  • `meta name="keywords"` `meta http-equiv="X-UA-Compatible"` `meta name="twitter:title"` `meta property="og:site_name"` ? – Kiquenet Oct 16 '18 at 12:09