Empty first row in razor mvc 4 rc

I switched from mvc 3 to mvc 4 and ran into the following problem.

@using InvoiceDocflow.Controllers @{ Response.ContentType = "text/xml"; } <?xml version="1.1" encoding="UTF-8" ?> <dc> @foreach (var dcLink in (IEnumerable<DcLink>)ViewData["SupportedDcs"]) { <link rel="@dcLink.RelUri.ToString()" href="@dcLink.DcUri.ToString()" /> } </dc> 

It's my opinion. My layout is just one line

 @RenderBody() 

So, in mvc 3 <?xml version="1.1" encoding="UTF-8" ?> Appeared on the first line, but now it appears on the second line, leaving the first line empty.

enter image description here

Can I render on the first line, as it was in mvc 3?

By the way.

 @using InvoiceDocflow.Controllers @{ Response.ContentType = "text/xml"; }<?xml version="1.1" encoding="UTF-8" ?> 

This will work, but that’s not what I want to do at all.

+8
razor asp.net-mvc-4
source share
8 answers

Temporary fix? ActionFilter and cross out the empty first line? It is clear that you could also perform another assessment in the answer, if appropriate.

 public class TranslationFilter : MemoryStream { private Stream filter = null; public TranslationFilter(HttpResponseBase httpResponseBase) { filter = httpResponseBase.Filter; } public override void Write(byte[] buffer, int offset, int count) { var response = UTF8Encoding.UTF8.GetString(buffer); // remove all newlines response = response.Replace(System.Environment.NewLine, ""); /* remove just first empty line if (response.Substring(0, 2) == "\r\n") { response = response.Substring(2, response.Length - 2); } */ filter.Write(UTF8Encoding.UTF8.GetBytes(response), offset, UTF8Encoding.UTF8.GetByteCount(response)); } } public class ResponseFilter : ActionFilterAttribute { public ResponseFilter() { } public override void OnResultExecuted(ResultExecutedContext filterContext) { base.OnResultExecuted(filterContext); filterContext.HttpContext.Response.Filter = new TranslationFilter(filterContext.HttpContext.Response); } } 

And add it to the Controller method?

 [ResponseFilter] public ActionResult Index() { return View(); } 
+6
source share

I had a similar problem with blank lines at the top of the page when trying to return a cache manifest file. Solution, add Response.Write ("..."), this will be on the first line of the page.

  @{ Layout = null; Response.Write("CACHE MANIFEST"); } ....... 
+6
source share

I know this is an old question, but I found it when I was looking for a solution to this problem. Since no one has an easy solution, here is mine:

I was able to fix this using a partial view instead of the usual view. I believe the problem boils down to the layout, even if you set it to zero, it still seems like it is adding an empty first line. @ {} Declares an empty string, so you must move it to the bottom of your view.

So, just add a partial view, and your controller will return a partial view as such:

 public ActionResult GenerateXML() { return PartialView("_XML"); } 

Then in your partial view, you will need to configure it as follows (using @ {} below):

 @model string <?xml version="1.0" encoding="UTF-8" ?> <response> <message>@Model</message> </response> @{ Response.ContentType = "text/xml";} 

This will lead to the HTML source:

 <?xml version="1.0" encoding="UTF-8" ?> <response> <message>Response Message</message> </response> 

I hope this helps others facing this issue.

+4
source share

I know that you suggested just putting the tag at the end of @{} , but why not post it before that. Moreover, is this empty space causing an error or problem? You can clearly see that this is due to spaces created by your code, not from mvc or razor.

Maybe something like this:

 @using InvoiceDocflow.Controllers <?xml version="1.1" encoding="UTF-8" ?> @{ Response.ContentType = "text/xml"; } 
+2
source share

None of the above solutions worked for me, therefore, having spent a lot of time fixing it, it should be very simple, since this is just an empty line - I wrote a blog post about this.

http://devstuffs.wordpress.com/2014/02/07/first-line-empty-in-razor/

+2
source share

I solved a similar problem as follows:

_ViewStart.cshtml

 @{ Layout = null; Response.Write("<!DOCTYPE html>"); } 

Another .cshtml

 @model ViewModel<EshopWebsiteOrder> <html> <head> <meta charset="utf-8" /> 

Finally, it makes sense to define a common DOCTYPE in one place.

+2
source share

Another simple fix without ResponseFilter. Just put @using at the bottom of the cshtml file:

 <?xml version="1.1" encoding="UTF-8" ?> <dc> @foreach (var dcLink in (IEnumerable<DcLink>)ViewData["SupportedDcs"]) { <link rel="@dcLink.RelUri.ToString()" href="@dcLink.DcUri.ToString()" /> } </dc> @using InvoiceDocflow.Controllers @{ Response.ContentType = "text/xml"; } 
+1
source share

If you move:

 <?xml version="1.1" encoding="UTF-8" ?> 

at the top of the layout template, then your xml page inherits and outputs your XML data that should work.

+1
source share

All Articles