2015-02-09 82 views
-1

我的页面有一个meta描述标签,我以编程方式更改的meta描述标签的内容是这样编程改变在C#中的meta描述

<meta name="description" content=<%=getmeta()%> /> 

的getmeta功能类似以下

public string getmeta() 
{ 
    return "this is a meta description's content"; 

} 

当我页面呈现它呈现这样毫无意义的meta元素

<meta name="description" content="this" is="" a="" meta="" description's="" content="" > 

什么是导致这个,我错过了什么?

+5

难道真的编译?在getmeta函数的return语句结尾处有多余的“)”。 – 2015-02-09 10:18:22

+0

一切都在这里投票!即使正确的答案! :) – 2015-02-09 10:47:34

+0

@DaveBecker哪个答案是正确的? – DavidG 2015-02-09 10:52:39

回答

-1

首先,你的代码无法编译,因为你有一个额外的“)”在你的方法到底:

return "this is a meta description's content"); 

但是,要回答你的问题,为元标记的内容应放置在双引号之间才能工作。

我的确建议你调整你的方法来返回一个包含双引号的字符串,但这需要转义你的双引号字符。

如果你修改你的方法类似如下它应该工作(测试&验证):

public string getmeta() 
{ 
    return "\"this is a meta description's content\""; 
} 

在HTML的输出然后将:

<meta name="description" content="this is a meta description's content" /> 
+0

你是不是把双引号放在标记中而不是用字符串搞乱? – DavidG 2015-02-09 10:27:23

+0

这只是我个人的工作方式。我不太喜欢这样,所以我会将它们添加到字符串本身中。 – Complexity 2015-02-09 10:29:30

-1

最好的办法是

protected void Page_Load(object sender, EventArgs e) 
{ 
    this.Page.Header.Controls.Add(new LiteralControl(@"<meta name='description' content='this is a meta description's content' />")); 
} 

或其他方式是 -

-Put PlaceHoled在您的.aspx header部分。

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

C#代码:

HtmlMeta meta = new HtmlMeta(); 
meta.Name = "Name1"; 
meta.Content = "this is a meta description's content"; 
MetaPlaceHolder.Controls.Add(meta); 
+0

如果您认为符合“downvote”的条件,请添加评论。 – prog1011 2015-02-09 10:36:45

+0

对于一开始,它不能解决问题,而您的“最佳”方法包含错误。 – DavidG 2015-02-09 10:50:26