您的位置:首页 > 编程语言 > ASP

asp.net MVC 如何在aspx页面的head标签里输出Key为常量的ViewData呢?

2011-11-08 17:35 477 查看
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标签里呢?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: