Razor ViewEngine是否缓存呈现的HTML? [英] Does Razor ViewEngine cache the rendered HTMLs?

查看:137
本文介绍了Razor ViewEngine是否缓存呈现的HTML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个2级菜单项:我有一个部门列表,每个部门都有一个商店列表.

I have a 2-level menu item: I have a list of department and each department has a list of stores.

我有一个PartialView菜单,它遍历Model(部门)并构建菜单:

I have a Menu, PartialView which iterates through the Model (departments) and builds the menu:

@model IEnumerable<Department>

<ul>
    @foreach (var department in Model)
    {
        <li>
            <a href="#">@Model.DepartmentName</a>
            <ul>
                @foreach (var store in department.Stores)
                {
                    <li><a href="some-url">@store.StoreName</a></li>
                }
            </ul>
        </li>
    }
</ul>

这就是我在_layout.cshtml中调用菜单PartialView的方式:

And this is how I call the Menu PartialView in my _layout.cshtml:

@Html.Partial("Shared/_Menu", MyApplicationCache.departments) 

如您所见,我在所有请求上都将相同的模型(从缓存中)传递给PartialView.

As you can see I am passing the same model (from the cache) to the PartialView on all the requests.

Razor ViewEngine是否具有内部缓存系统来识别该视图已经为此模型建立(已转换为HTML字符串)?还是在每个单个请求上重新渲染(重新编译)PartialView?

Does Razor ViewEngine have an internal caching system to recognize that this view has already been built (complied to HTML string) for this model? Or does it re-render (recompile) the PartialView on every single request?

推荐答案

假设您没有对Controller或其操作方法应用任何OutputCacheAttribute,则每次请求时都会重新渲染PartialView参与其中.

The PartialView gets re-rendered at every single request, assuming you don't have any OutputCacheAttribute applied on the Controller or its action method involved.

如果需要输出缓存,则需要通过OutputCacheAttribute显式设置,请参见文档.

If you need output caching, you need to set this up explicitly via OutputCacheAttribute, see the documentation.

您可以通过输出DateTime轻松地进行检查,例如.通过菜单项(如下所示).
在每次请求时,它将显示一个新值,证明它已被重新渲染.

You can easily check this by outputting a DateTime, eg. via a menu-item as shown here below.
At every request, it will show a new value, proving it got re-rendered.

<li><a href="#">@DateTime.Now</a></li>

完整菜单:

@model IEnumerable<Department>

<ul>
    @foreach (var department in Model)
    {
        <li>
            <a href="#">@Model.DepartmentName</a>
            <ul>
                <li><a href="#">@DateTime.Now</a></li>
                @foreach (var store in department.Stores)
                {
                    <li><a href="some-url">@store.StoreName</a></li>
                }                
            </ul>
        </li>
    }
</ul>

这篇关于Razor ViewEngine是否缓存呈现的HTML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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