ASP.NET MVC 4 and Razor 2: Viewer no longer supports xml?

We had a view (.cshtml) that displayed XML for the RSS feed using ASP.NET MVC 3, which worked fine. Now that we have upgraded ASP.NET MVC 4 using Razor 2, it generates compilation errors similar to the ones below.

Parser Error Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately. Parser Error Message: Encountered end tag "item" with no matching start tag. Are your start/end tags properly balanced? 

Tags are correctly balanced.

Anyone have any thoughts?

UPDATE: I highlighted it using the link element inside the item element in @foreach (...) {...}.

 @foreach (var item in Model.Items) { <item> <title>@item.Title</title> <link>@item.Link</link> <description>@item.Description</description> <guid>@item.Guid</guid> @if (item.PublishedDateUtc.HasValue) { <pubDate>@item.PublishedDateUtc.Value.ToString("ddd, dd MMM yyyy HH:mm:ss") GMT</pubDate> } </item> } 

I fixed it using @ Html.Raw below.

 @foreach (var item in Model.Items) { <item> <title>@item.Title</title> @Html.Raw(string.Format("<link>{0}</link>", item.Link.ToHtmlEncoded())) <description>@item.Description</description> <guid>@item.Guid</guid> @if (item.PublishedDateUtc.HasValue) { <pubDate>@item.PublishedDateUtc.Value.ToString("ddd, dd MMM yyyy HH:mm:ss") GMT</pubDate> } </item> } 

Does anyone have any better suggestions? Obviously, I could just use the class to declare the model and return the XML directly from the controller, but I'm more interested in why this happens and what I can do to better match the Razor syntax.

+6
source share
1 answer

When I first looked at this, I noticed that the link tag had a closing tag instead of self-closing. Razor is smart enough to know html and knows how the link tag is closed. Here is another example of what might break to show how Razor reads html.

 .... <tbody> @if (alternating) { <tr class='alternating'> } else { <tr> } .... </tr> </tbody> 

This will fail because it sees </tr> without an open tag.

Tags that always close are interpreted by Razor as self-closing. therefore, the link tag actually ended in > . (since it has valid html to have a closing tag without /> ). So, now we are faced with a closing tag without an open tag in order to justify it, so that the parser refuses and says that it is not formatted properly. I will need to do some verification, but I'm sure the closing link tag is supposed to be for the item tag, as this will give the right balance, so the rest of the file is perfectly parsed until it encounters this single item tag without opening the tag.

This should be smart enough to know that the previous closing tag was incorrect. Perhaps this is a problem with the team.

I have not tried, but you must do it

 @foreach (var item in Model.Items) { <item> <title>@item.Title</title> @:<link> @item.Link @:</link> <description>@item.Description</description> <guid>@item.Guid</guid> @if (item.PublishedDateUtc.HasValue) { <pubDate>@item.PublishedDateUtc.Value.ToString("ddd, dd MMM yyyy HH:mm:ss") GMT</pubDate> } </item> } 
+8
source

Source: https://habr.com/ru/post/917519/


All Articles