ASP.Net MVC 3 Razor Response.Write 位置

发布于 2024-10-31 13:20:36 字数 1331 浏览 1 评论 0原文

我正在尝试更新本教程< /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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

诗酒趁年少 2024-11-07 13:20:36

Razor 视图由内向外渲染。基本上,它将内容写入临时缓冲区,当到达最顶层的布局页面时,临时缓冲区会写入响应流。因此,从 HtmlHelper 扩展直接写入响应流将导致乱序输出。

解决方案是使用:

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

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>");
迟到的我 2024-11-07 13:20:36

将您的方法更改为非 void,但返回 MvcHtmlString

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

比像以前那样使用它

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

Idea 的灵感来自于 MVC 中几乎每个输入(和其他)扩展方法都返回 MvcHtmlString

Change your method to be not void, but returning 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

简单爱 2024-11-07 13:20:36

您应该使用 ViewBag 并将字符串放入其中,然后输出它。

在控制器中:

ViewBag.Foo = Bar;

在视图中:

<div>
@ViewBag.Foo
</div>

You should use the ViewBag and put the string in there, then output it.

In controller:

ViewBag.Foo = Bar;

In view:

<div>
@ViewBag.Foo
</div>
奶茶白久 2024-11-07 13:20:36

我懒得这么做。您想要的最终结果是没有将 FooBar 写入您的 div 中,那么为什么不将其写入您的 div 中呢?为什么需要使用Response.Write

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? Why do you need to use Response.Write?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文