ASP.Net MVC 3 Razor Response.Write 位置
我正在尝试更新本教程< /a> 将 Facebook 的 BigPipe 实施到 razor。
有一个 html 帮助器扩展,可以将 pagelet 添加到列表中,然后将保持 div 输出到响应。这个想法是,稍后将此 pagelet 的内容呈现为字符串,然后通过 javascript 注入到此控股 div 中。
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>");
}
在示例中,该函数的调用方式如下:
<div id="textHolder">
<% Html.RegisterPagelet(myPagelet); %>
</div>
它将 pagelet 添加到列表中,并将持有的 div 输出到响应流。
但是
<div id="textHolder">
<div id="pageletPlaceHolder"></div>
</div>
,当我在 Razor 中尝试相同的操作时:
<div id="textHolder">
@{ Html.RegisterPagelet(myPagelet); }
</div>
div 占位符出现在正文的顶部,位于 textHolder div 之外。这是为什么呢?我怎样才能让它表现得像 webforms 视图,其中响应在 div 内输出?
谢谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Razor 视图由内向外渲染。基本上,它将内容写入临时缓冲区,当到达最顶层的布局页面时,临时缓冲区会写入响应流。因此,从 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.
The solution is to use:
将您的方法更改为非 void,但返回 MvcHtmlString
比像以前那样使用它
Idea 的灵感来自于 MVC 中几乎每个输入(和其他)扩展方法都返回 MvcHtmlString
Change your method to be not void, but returning MvcHtmlString
Than use this as you used to
Idea is inspired by the fact that almost every input(and other) extension method in MVC returns MvcHtmlString
您应该使用 ViewBag 并将字符串放入其中,然后输出它。
在控制器中:
在视图中:
You should use the ViewBag and put the string in there, then output it.
In controller:
In view:
我懒得这么做。您想要的最终结果是没有将
FooBar
写入您的div
中,那么为什么不将其写入您的 div 中呢?为什么需要使用Response.Write
?I wouldn't bother doing it. The end result you want is t have
FooBar
written within yourdiv
, so why not just write it into your div? Why do you need to useResponse.Write
?