views:

227

answers:

2

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?

+2  A: 

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.

Steve Michelotti
@Steve: So removing the `runat="server"` attribute from the head won't affect the server side script rendering? Seems odd.
James
The runat="server" attribute is used for traditional asp.net web forms such that the server side C# code behind files can "get at" the html elements. asp.net MVC doesn't use this at all - there isn't even a runat="server" attribute in the <form> element. MVC does however, use the web forms view engine so you'll still see remnants of this - most visible is probably how the master page content placeholders still have the runat="server" attribute. And even this will go away when Razor drops in MVC 3. Bottom line, you don't need it for something like this in MVC.
Steve Michelotti
+1  A: 

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.

http://stackoverflow.com/questions/987535/asp-net-webform-css-link-getting-mangled

Kelsey
@Kelsey: Thanks for the background info. I actually recall for the Keywords one I was doing something like - `<meta name="keywords" content="<%= String.Join(", ", Model.keywords.ToArray()) %>" />` when my model was an array and it was actually working ok. However, as now it's just a string it is doing this! What about Steve's answer, does that seem safe enough to do? I have applied the change and it is working fine, just wondering if there are any implications by not supplying the `runat` attribute.
James
@James I would not add the `runat="server"` tag just because you are using MVC which really stays away from any server side controls or the use of `runat` even though you won't be using it. It's overhead you just don't need since the inline code will work. I am not sure if it works either as I have not tested it.
Kelsey