asp.net MVC 如何在aspx頁面的head標籤裡輸出Key為常量的ViewData呢?
下面是有效:
<meta name="description" content="<%:ViewData["PageDescription"]%>" />
直接放在head裡,有效:
<%:ViewData[BaseConstant.STR_VIEWDATA_PAGEDESCRIPTION]%>
下面使用了常量來代替"PageDescription"
常量定義:public const string STR_VIEWDATA_PAGEDESCRIPTION = "PageDescription";
把下列語句放在body標籤裡,viewdata能正常輸出:
<meta name="description" content= "<%:ViewData[BaseConstant.STR_VIEWDATA_PAGEKEYWORD]%>" />
同樣的語句放在head標籤裡,viewdata卻無法正常輸出:
<meta name="description" content= "<%:ViewData[BaseConstant.STR_VIEWDATA_PAGEKEYWORD]%>" />
直接輸出如下:
<meta name="description" content="<%:ViewData[BaseConstant.STR_VIEWDATA_PAGEKEYWORD]%>" />
為什麼會這樣?如何在aspx頁面的head標籤裡輸出Key為常量的ViewData呢?
把head標籤裡的 runat="server" 刪除就可以使用了。
change from
<head runat="server">
<meta name="description" content= "<%:ViewData[BaseConstant.STR_VIEWDATA_PAGEKEYWORD]%>" />
to
<head>
<meta name="description" content= "<%:ViewData[BaseConstant.STR_VIEWDATA_PAGEKEYWORD]%>" />
....
微軟在搞什麼啊??原因是什麼呢?bug?
既然是MVC4,那為什麼建立masterpage的時候會runat="server"加到head標籤裡呢?