简体   繁体   English

ASP.NET MVC 3 Razor输出缓冲区视图吗?

[英]Does ASP.NET MVC 3 Razor output buffer views?

In ASP.NET MVC 3 Razor, you can specify the page title with: 在ASP.NET MVC 3 Razor中,您可以使用以下命令指定页面标题:

@{
    ViewBag.Title = "Title";
}

Now, suppose we have a layout page with: 现在,假设我们有一个布局页面:

<title>@ViewBag.Title | Website</title>

When ASP goes to render a page, it will need to output some of the layout page HTML, then the view HTML, then the rest of the layout page HTML. 当ASP去渲染页面时,它需要输出一些布局页面HTML,然后是视图HTML,然后是布局页面HTML的其余部分。

In order to output the first half of the layout page HTML, ASP.NET will need to know the value given to ViewBag.title in the view. 为了输出布局页面HTML的前半部分,ASP.NET需要知道视图中给ViewBag.title赋予的值。 Thus, ASP.NET needs to parse the Razor code in the view. 因此,ASP.NET需要在视图中解析Razor代码。 However, ASP.NET can't output the view's HTML code just yet because it is still outputting the layout page's HTML code. 但是,ASP.NET还不能输出视图的HTML代码,因为它仍在输出布局页面的HTML代码。 So does ASP.NET store the HTML output of the view in a buffer? 那么ASP.NET是否将视图的HTML输出存储在缓冲区中? That seems like a bad practice, but I can't think of any other way to efficiently get the view's title into the layout page output. 这似乎是一种不好的做法,但我想不出有任何其他方法可以有效地将视图的标题放入布局页面输出中。

When ASP goes to render a page, it will need to output some of the layout page HTML, then the view HTML, then the rest of the layout page HTML. 当ASP去渲染页面时,它需要输出一些布局页面HTML,然后是视图HTML,然后是布局页面HTML的其余部分。

That's how web forms are rendered. 这就是Web表单的呈现方式。 However due to the issue you describe (along with a few others) mvc renders inside out. 但是由于你描述的问题(以及其他一些)mvc内部渲染。

So the inner view is rendered first to a temporary buffer. 因此,内部视图首先呈现为临时缓冲区。 Then the layout page. 然后是布局页面。 This rendering continues until the outer most layout page is reached when the buffer is then wrote to the response stream and flushed. 当缓冲区随后写入响应流并刷新时,此呈现将继续,直到到达最外层布局页面。

This doesn't cause any issues 90% of the time (guestimate) however this will cause headaches if you ever need to flush the response early. 这不会导致任何问题90%的时间(猜测)但是如果您需要提前清除响应,这将导致头痛。

FYI the buffer for the views can be accessed with: 仅供参考,可以使用以下方式访问视图的缓冲区:

HtmlHelper.ViewContext.Writer

So to answer your question, yes - it does buffer views. 所以回答你的问题,是的 - 它确实缓冲了视图。

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

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