ASP.NET MVC2 Master Page - Server side script not rendering, first bracket being escaped

六月ゝ 毕业季﹏ 提交于 2019-12-23 13:07:15

问题


I have a master page which I am using as a template to allow me to define meta tags per page. My master page takes in a model which contains the meta information, here is an example of what I am trying to do the following:

<meta name="description" content="<%= Model.description %>" />
<meta name="keywords" content="<%= Model.keywords %>" />

However, when I check the HTML once the page is rendered I get this:

<meta name="description" content="&lt;= Model.description %>" />
<meta name="keywords" content="&lt;= Model.keywords %>" />

If I remove the outer quotation marks from the content e.g. content=<%= Model.description %> it renders the data. It doesn't seem to like the surrounding quotation marks.

Is this a bug with the master pages? If so, what would be the best alternative workaround for this? If not, what am I doing wrong?


回答1:


I have seen that before and it is a pain. Probably you have a runat="sever" attribute in your head tag like this:

<head runat="server">

if you just made it:

<head>

then you should not see this behavior.




回答2:


This has always been an issue because it is trying to encode the contents in the attributes. You can get around it by doing this instead:

<%= string.Format("<meta name=\"description\" content=\"{0}\" />", Model.description) %> 
<%= string.Format("<meta name=\"keywords\" content=\"{0}\" />", Model.keywords) %> 

EDIT: This is not an issue specific to MasterPages. I posted a similar question a long time ago here on SO asking about it and if you read the accepted answer you can see that the framework has specific code for various items in the head tag where it will have a slightly different rendering format and will encode the data.

ASP.NET Webform css link getting mangled



来源:https://stackoverflow.com/questions/3689494/asp-net-mvc2-master-page-server-side-script-not-rendering-first-bracket-being

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!