2015-02-09 19 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