2011-06-01 106 views

回答

18

没有和刷新尝试这样做,但一般可以加元标记是这样的:

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

更新:有可能这样。检查里克施特拉尔 http://www.west-wind.com/weblog/posts/2006/Aug/04/No-more-Meta-Refresh-Tags

+1

我使用的解决方案,你的链接里克斯特拉斯博客;它工作得很好。非常感谢。 – doogdeb 2011-06-01 10:08:02

+0

我试着按照上面的方法做如下操作: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);给我一个错误:无法修改控件集合。您的帮助将不胜感激。 – Ruruboy 2013-12-17 19:19:50

3

您可以在html头部的母版页上添加内容占位符。然后,您可以将内容添加到特定内容页面中的内容部分,并将其输出到页眉。

16

This page解释了新的功能:ASP.Net 4增加了2个新的Meta标记相关的属性页。它们可以用于为关键字和描述设置元标记。

您可以在代码中设置它们背后:

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

您还可以在@ Page指令设置:

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

的任一方法的输出中呈现HTML类似于以下内容:

<!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> 
+1

你不能添加像这样的刷新。 – Pleun 2011-06-01 09:24:40

+1

+1不知道这是ASP.NET 4的可能! – Curt 2012-03-07 12:20:57

+0

不回答问题。 – 2013-04-26 16:28:53

3
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

4

在设计页面中添加以下代码现在

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

下面的代码添加到您的.cs页

Page.MetaKeywords = "keyword1, keyword2, keyword3"; 
    Page.MetaDescription = "Example of new meta tag"; 
+1

不需要将元标记添加到Site.Master即可让代码继续工作。 – Brent 2013-12-06 17:37:21

+1

一旦你添加到aspx文件并保存它,它会自动添加到visual studio中的设计器文件 – 2016-07-01 09:10:52

2

的一种方式,我发现做到这一点(即我没有在这里列出)是有一个文字,并填写任何类型的元标签你想要的。就我而言,我需要使用它没有主页,有Facebook的识别缩略图,标题和描述:

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

代码隐藏:

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; 
相关问题