问题
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="<= Model.description %>" />
<meta name="keywords" content="<= 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