Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net MVC 3 Razor Response.Write position

I am trying to update this tutorial on implementing Facebooks BigPipe to razor.

There is a html helper extension that adds a pagelet to a list, and then outputs a holding div to the response. The idea is that later on the content of this pagelet is rendered to a string, and then injected into this holding div via javascript.

public static void RegisterPagelet(this HtmlHelper helper, Pagelet pagelet) {
    var context = helper.ViewContext.HttpContext;
    List<Pagelet> pagelets = (List<Pagelet>)context.Items["Pagelets"];
    if (pagelets == null) {
        pagelets = new List<Pagelet>();
        context.Items["Pagelets"] = pagelets;
    }
    pagelets.Add(pagelet);
    context.Response.Write("<div id=\"" + pagelet.Container + "\"></div>");
}

In the example this function is called like this:

<div id="textHolder">
    <% Html.RegisterPagelet(myPagelet); %>
</div>

Which adds the pagelet to the lists, and outputs the holding div to the response stream.

So

<div id="textHolder">
    <div id="pageletPlaceHolder"></div>
</div>

However, when I try the same in Razor:

<div id="textHolder">
    @{ Html.RegisterPagelet(myPagelet);  }
</div>

The div placeholder appears at the top of the body, outside the textHolder div. Why is this? How can I get this to behave like the webforms view where the response is output inside the div?

Thanks.

like image 343
Terry Avatar asked Apr 10 '11 10:04

Terry


People also ask

How can you comment using Razor syntax in MVC?

Comments Razor View Engine has two types of comments, one is single-line and another is multiline. Razor uses the syntax "@* .. *@" for the comment block but in a C# code block we can also use "/* */" or "//".

How do you comment in a Razor file?

How to comment in razor view? To comment in the code block of Razor view, we use the same syntax as we use in C#. Like for single line // and for multiline /* and */. To comment, HTML along with other code, we need to use razor comment block that starts with @* and ends with *@.

What is the difference between Razor and Cshtml?

razor helps you embed serverside code like C# code into web pages. cshtml is just a file extension. razor view engine is used to convert razor pages(. cshtml) to html.

Can you mix MVC and Razor pages?

You can add support for Pages to any ASP.NET Core MVC app by simply adding a Pages folder and adding Razor Pages files to this folder. Razor Pages use the folder structure as a convention for routing requests.


1 Answers

A Razor view is rendered inside-out. Basically it writes content to temporary buffers which get written to the response stream when the top most layout page is reached. Thus, writing directly to the response stream from your HtmlHelper extension, will output it out of order.

The solution is to use:

helper.ViewContext.Writer.Write("<div id=\"" + pagelet.Container + "\"></div>");
like image 176
James Hull Avatar answered Sep 16 '22 20:09

James Hull