ASP.Net MVC 3剃须刀的Response.Write位置 [英] ASP.Net MVC 3 Razor Response.Write position

查看:477
本文介绍了ASP.Net MVC 3剃须刀的Response.Write位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图更新本教程关于实施Facebook的BigPipe到剃须刀。

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

有是,增加了一个小网页到列表,并且然后输出一个保持div来响应一个HTML辅助延伸。这个想法是,后来这个小页的内容呈现为一个字符串,然后注入通过javascript这个拿着DIV。

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>

它增加了小网页的名单,并输出控股div来响应流。

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

所以

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

然而,当我尝试同样的剃刀:

However, when I try the same in Razor:

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

的格占位符显示在本体的顶部,textHolder股利外部。为什么是这样?我怎样才能得到这个行为类似于web表单视图,在响应是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?

感谢。

推荐答案

剃刀视图内而外呈现。基本上,它写的内容其中获得达到最顶端的布局页面时写入响应流临时缓冲区。因此,直接写入到从您的HtmlHelper扩展响应流,将其输出失灵了。

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.

的解决方案是使用

helper.ViewContext.Writer.Write("<div id=\"" + pagelet.Container + "\"></div>");

这篇关于ASP.Net MVC 3剃须刀的Response.Write位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆