Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Empty first line in razor mvc 4 rc

I've migrated from mvc 3 to mvc 4 and encountered with 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>

This is my view. My layout is just one line

@RenderBody()

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

enter image description here

Can I make it 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 would work, but this is not what I whant to do at all.

like image 753
er-v Avatar asked Jul 04 '12 11:07

er-v


People also ask

What is@: in Razor?

Razor supports C# and uses the @ symbol to transition from HTML to C#. Razor evaluates C# expressions and renders them in the HTML output.

What is meaning of@: in. net MVC?

In MVC, @ is the respective char that allows you to use razor inside HTML (inside a . cshtml) which in runtime (or precompiled) will be converted to c#. With @ you may write C# within HTML and with @: you may write HTML within C#. Example: @foreach (TestClass item in Model) { @:@item.Code - @item.Name }

What is Razor syntax in MVC?

Razor is a markup syntax that lets you embed server-based code into web pages using C# and VB.Net. It is not a programming language. It is a server side markup language. Razor has no ties to ASP.NET MVC because Razor is a general-purpose templating engine. You can use it anywhere to generate output like HTML.

What is Razor syntax?

Razor syntax is a simple programming syntax for embedding server-based code in a web page. In a web page that uses the Razor syntax, there are two kinds of content: client content and server code.


2 Answers

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

 @{
     Layout = null;
     Response.Write("CACHE MANIFEST");

} .......
like image 58
Derrick Hampton Avatar answered Sep 22 '22 01:09

Derrick Hampton


Temporary fix? ActionFilter and strip out the empty first line? Clearly you could also do other minification on the response if suitable.

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 onto the Controller method?

[ResponseFilter]
public ActionResult Index()
{
return View();
}
like image 26
Phil Avatar answered Sep 23 '22 01:09

Phil