简体   繁体   English

ASP.Net MVC 3 Razor Response.Write位置

[英]ASP.Net MVC 3 Razor Response.Write position

I am trying to update this tutorial on implementing Facebooks BigPipe to razor. 我正在尝试更新关于将Facebook BigPipe实施到剃须刀的教程

There is a html helper extension that adds a pagelet to a list, and then outputs a holding div to the response. 有一个html助手扩展,它将一个pagelet添加到列表中,然后将一个保持div输出到响应中。 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. 我的想法是,稍后将此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>");
}

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. 这会将小页面添加到列表中,并将保留div输出到响应流。

So 所以

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

However, when I try the same in Razor: 但是,当我在Razor中尝试相同时:

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

The div placeholder appears at the top of the body, outside the textHolder div. div占位符显示在bodyHolder div之外的正文顶部。 Why is this? 为什么是这样? How can I get this to behave like the webforms view where the response is output inside the div? 我怎样才能使这个行为像在div中输出响应的webforms视图?

Thanks. 谢谢。

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. 因此,直接写入来自HtmlHelper扩展的响应流,将无序输出。

The solution is to use: 解决方案是使用:

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

Change your method to be not void, but returning MvcHtmlString 将您的方法更改为无效,但返回MvcHtmlString

public static MvcHtmlString OutputText(this HtmlHelper helper, string text) {
     return New MvcHtmlString(text);
}

Than use this as you used to 比以往使用这个

<div id="textHolder">
    @Html.OutputText("FooBar");
</div>

Idea is inspired by the fact that almost every input(and other) extension method in MVC returns MvcHtmlString Idea的灵感来自于MVC中的几乎所有输入(和其他)扩展方法都返回MvcHtmlString

You should use the ViewBag and put the string in there, then output it. 您应该使用ViewBag并将字符串放在那里,然后输出它。

In controller: 在控制器中:

ViewBag.Foo = Bar;

In view: 在视图中:

<div>
@ViewBag.Foo
</div>

I wouldn't bother doing it. 我不打算这样做。 The end result you want is t have FooBar written within your div , so why not just write it into your div? 你想要的最终结果是你的div写了FooBar ,为什么不把它写进你的div呢? Why do you need to use Response.Write ? 为什么需要使用Response.Write

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM