Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Razor 2 to write RSS fails on the <link> element

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?

like image 957
Drew Noakes Avatar asked Oct 03 '12 15:10

Drew Noakes


2 Answers

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>
like image 56
OakNinja Avatar answered Sep 18 '22 15:09

OakNinja


For now I go with this workaround:

 @Html.Raw(string.Format(@"<param name=""{0}"">{1}</param>",Name, Value)) 
like image 29
Alexander Taran Avatar answered Sep 21 '22 15:09

Alexander Taran