5

Closed

Razor outputs blank lines

description

Create a controller and a razor view. In controller, set Response.ContentType = "text/xml".
In view, put
@{
Layout = null;
}
<?xml version="1.0" encoding="utf-8" ?>

Now run the view. No matter what you do the <?xml block will be on the 2nd line output, which of course is an invalid xml document.

Enter webforms pages. Create a view containing this all on first line in view:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" ContentType="text/xml" %><?xml version="1.0" encoding="utf-8" ?>

Webforms works and does not output extra lines - Razor does. Appears there is blank at the top and everytime there is @{} block. Here is another user reporting. http://stackoverflow.com/questions/11327787/empty-first-line-in-razor-mvc-4-rc
Closed Apr 19 at 5:47 PM by yishaigalatzer
Thanks again for reporting this bug.
We spent some time digging into this, and we believe that removing the whitespace was the right thing to do, fixing it today will be a potential breaking change for any application relying (inadvertently or not) on the spacing Razor currently injects into the document.

comments

eilonlipton wrote Apr 15 at 11:07 PM

Thank you for reporting this bug. For now we recommend placing the XML declaration above the @{} code block. This will cause Razor to render that line first.