I upgraded to MVC 4 yesterday and just discovered a bug that the upgrade introduced.
I have a Razor view that is used to generate an RSS feed. It has some markup like this (simplified):
<item>
<title>@post.BlogPost.Title</title>
<link>@Url.BlogPost(post.BlogPost, isAbsolute: true)</link>
</item>
In Razor version two, there's special support for HTML5 void elements. Such void elements are self closing, and do not have a closing tag.
Unfortunately, <link>
is one such element.
This means the above Razor markup is no longer valid, and fails at runtime. Removing the closing </link>
tag removes the parser error, but means that it's no longer valid RSS.
So, is there a way to get around this, or is Razor only really suitable for generating HTML5?
I'd do it like this:
<item>
<title>
@post.BlogPost.Title
</title>
@Html.Raw("<link>")
@Url.BlogPost(post.BlogPost, isAbsolute: true)
@Html.Raw("</link>")
</item>
Generated source will look like this:
<item>
<title>
Google
</title>
<link>
http://www.google.se
</link>
</item>
For now I go with this workaround:
@Html.Raw(string.Format(@"<param name=""{0}"">{1}</param>",Name, Value))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With