Razor视图引擎.cshtml页面中的OutputCache

时间:2021-07-02 15:12:27

Using ASP.NET MVC web forms we can put output cache either in controller level or view level. How can we mention "outputcache" in .cshtml page?

使用ASP.NET MVC Web表单,我们可以将输出缓存放在控制器级别或视图级别。我们怎么能在.cshtml页面中提到“outputcache”?

I did not find it anywhere. Where can I get the syntax?

我没找到任何地方。我在哪里可以获得语法?

4 个解决方案

#1


7  

What do you mean "ASP.NET MVC Web Forms"? If you're referring to the OutputCache attribute in the Page directive, that is ASP.NET Web Forms.

你是什​​么意思“ASP.NET MVC Web Forms”?如果您在Page指令中引用OutputCache属性,那就是ASP.NET Web窗体。

ASP.NET MVC has Output Caching on the controller action level:

ASP.NET MVC在控制器操作级别上具有输出缓存:

    [OutputCache(Duration=10, VaryByParam="none")]
    public ActionResult Index()
    {
        return View();
    }

This is irrespective of the view engine (ASPX/Razor).

这与视图引擎(ASPX / Razor)无关。

#2


2  

Using ASP.NET MVC web forms you can can put output cache on the view level but this wouldn't have effect. It's there because it's an heritage from classic ASP.NET. In ASP.NET MVC the output cache should always be placed on the controller action.

使用ASP.NET MVC Web表单,您可以将输出缓存放在视图级别,但这不会起作用。它就在那里,因为它是经典ASP.NET的遗产。在ASP.NET MVC中,输出缓存应始终放在控制器操作上。

Because putting cache values in the view makes no sense in the newly introduced Razor view engine there's not such possibility. You should always put this attribute on the controller action.

因为在新引入的Razor视图引擎中将缓存值放在视图中是没有意义的,所以不存在这种可能性。您应该始终将此属性放在控制器操作上。

#3


2  

Refer to the latest post by Master Gu on this subject: MVC2 Announcement

请参阅谷师傅关于此主题的最新文章:MVC2公告

Particulary this part:

特别是这一部分:

Output Caching Improvements

输出缓存改进

ASP.NET MVC 3’s output caching system no longer requires you to specify a VaryByParam property when declaring an [OutputCache] attribute on a Controller action method. MVC3 now automatically varies the output cached entries when you have explicit parameters on your action method – allowing you to cleanly enable output caching on actions using code like below:

ASP.NET MVC 3的输出缓存系统不再要求您在Controller操作方法上声明[OutputCache]属性时指定VaryByParam属性。当你的action方法有明确的参数时,MVC3现在会自动改变输出缓存的条目 - 允许你使用如下代码干净地启用动作的输出缓存:

Razor视图引擎.cshtml页面中的OutputCache

In addition to supporting full page output caching, ASP.NET MVC 3 also supports partial-page caching – which allows you to cache a region of output and re-use it across multiple requests or controllers. The [OutputCache] behavior for partial-page caching was updated with RC2 so that sub-content cached entries are varied based on input parameters as opposed to the URL structure of the top-level request – which makes caching scenarios both easier and more powerful than the behavior in the previous RC.

除了支持整页输出缓存之外,ASP.NET MVC 3还支持部分页面缓存 - 它允许您缓存输出区域并在多个请求或控制器之间重用它。部分页面缓存的[OutputCache]行为使用RC2进行更新,以便子内容缓存条目根据输入参数而不是*请求的URL结构而变化 - 这使得缓存方案更容易,更强大在以前的RC中的行为。

So this improves things a lot for us.

所以这对我们来说改善了很多。

  1. Simply mentioning OutputCache on a controller action will take care of cashing the result from that particular Action for the defined duration. The cache will automatically be varied by the defined action parameters (which is normally the desired behavior.)
  2. 只需在控制器操作上提及OutputCache,即可在定义的持续时间内从特定操作中兑现结果。缓存将根据定义的操作参数(通常是所需的行为)自动更改。

  3. It will also work transparently on Child Actions (the ones invoked via Html.Action(...))
  4. 它也将在Child Actions(通过Html.Action(...)调用的那些)上透明地工作

#4


1  

Sounds as though others have answered the main question which is - do not configure page caching on the page / cshtml file in MVC3+, use the Action method in the controller.

听起来好像其他人回答了主要问题 - 不要在MVC3 +中的页面/ cshtml文件上配置页面缓存,请使用控制器中的Action方法。

However, for more complex scenarios you can access the WebCache object through the Razor syntax.

但是,对于更复杂的方案,您可以通过Razor语法访问WebCache对象。

Some of those scenarios are the old Doughnut / Doughnut (or Donut / Dounut) caching. An MVC3 focused thread here on Stack Overflow.

其中一些场景是旧的甜甜圈/甜甜圈(或Donut / Dounut)缓存。 Stack Overflow上的一个MVC3聚焦线程。

Also found a NuGet package MvcDonutCaching mentioned by Denis Huvelle which solves the problem for 3 & 4 - but I haven't tested it.

还发现了Denis Huvelle提到的NuGet包MvcDonutCaching,解决了3和4的问题 - 但我还没有测试过。

#1


7  

What do you mean "ASP.NET MVC Web Forms"? If you're referring to the OutputCache attribute in the Page directive, that is ASP.NET Web Forms.

你是什​​么意思“ASP.NET MVC Web Forms”?如果您在Page指令中引用OutputCache属性,那就是ASP.NET Web窗体。

ASP.NET MVC has Output Caching on the controller action level:

ASP.NET MVC在控制器操作级别上具有输出缓存:

    [OutputCache(Duration=10, VaryByParam="none")]
    public ActionResult Index()
    {
        return View();
    }

This is irrespective of the view engine (ASPX/Razor).

这与视图引擎(ASPX / Razor)无关。

#2


2  

Using ASP.NET MVC web forms you can can put output cache on the view level but this wouldn't have effect. It's there because it's an heritage from classic ASP.NET. In ASP.NET MVC the output cache should always be placed on the controller action.

使用ASP.NET MVC Web表单,您可以将输出缓存放在视图级别,但这不会起作用。它就在那里,因为它是经典ASP.NET的遗产。在ASP.NET MVC中,输出缓存应始终放在控制器操作上。

Because putting cache values in the view makes no sense in the newly introduced Razor view engine there's not such possibility. You should always put this attribute on the controller action.

因为在新引入的Razor视图引擎中将缓存值放在视图中是没有意义的,所以不存在这种可能性。您应该始终将此属性放在控制器操作上。

#3


2  

Refer to the latest post by Master Gu on this subject: MVC2 Announcement

请参阅谷师傅关于此主题的最新文章:MVC2公告

Particulary this part:

特别是这一部分:

Output Caching Improvements

输出缓存改进

ASP.NET MVC 3’s output caching system no longer requires you to specify a VaryByParam property when declaring an [OutputCache] attribute on a Controller action method. MVC3 now automatically varies the output cached entries when you have explicit parameters on your action method – allowing you to cleanly enable output caching on actions using code like below:

ASP.NET MVC 3的输出缓存系统不再要求您在Controller操作方法上声明[OutputCache]属性时指定VaryByParam属性。当你的action方法有明确的参数时,MVC3现在会自动改变输出缓存的条目 - 允许你使用如下代码干净地启用动作的输出缓存:

Razor视图引擎.cshtml页面中的OutputCache

In addition to supporting full page output caching, ASP.NET MVC 3 also supports partial-page caching – which allows you to cache a region of output and re-use it across multiple requests or controllers. The [OutputCache] behavior for partial-page caching was updated with RC2 so that sub-content cached entries are varied based on input parameters as opposed to the URL structure of the top-level request – which makes caching scenarios both easier and more powerful than the behavior in the previous RC.

除了支持整页输出缓存之外,ASP.NET MVC 3还支持部分页面缓存 - 它允许您缓存输出区域并在多个请求或控制器之间重用它。部分页面缓存的[OutputCache]行为使用RC2进行更新,以便子内容缓存条目根据输入参数而不是*请求的URL结构而变化 - 这使得缓存方案更容易,更强大在以前的RC中的行为。

So this improves things a lot for us.

所以这对我们来说改善了很多。

  1. Simply mentioning OutputCache on a controller action will take care of cashing the result from that particular Action for the defined duration. The cache will automatically be varied by the defined action parameters (which is normally the desired behavior.)
  2. 只需在控制器操作上提及OutputCache,即可在定义的持续时间内从特定操作中兑现结果。缓存将根据定义的操作参数(通常是所需的行为)自动更改。

  3. It will also work transparently on Child Actions (the ones invoked via Html.Action(...))
  4. 它也将在Child Actions(通过Html.Action(...)调用的那些)上透明地工作

#4


1  

Sounds as though others have answered the main question which is - do not configure page caching on the page / cshtml file in MVC3+, use the Action method in the controller.

听起来好像其他人回答了主要问题 - 不要在MVC3 +中的页面/ cshtml文件上配置页面缓存,请使用控制器中的Action方法。

However, for more complex scenarios you can access the WebCache object through the Razor syntax.

但是,对于更复杂的方案,您可以通过Razor语法访问WebCache对象。

Some of those scenarios are the old Doughnut / Doughnut (or Donut / Dounut) caching. An MVC3 focused thread here on Stack Overflow.

其中一些场景是旧的甜甜圈/甜甜圈(或Donut / Dounut)缓存。 Stack Overflow上的一个MVC3聚焦线程。

Also found a NuGet package MvcDonutCaching mentioned by Denis Huvelle which solves the problem for 3 & 4 - but I haven't tested it.

还发现了Denis Huvelle提到的NuGet包MvcDonutCaching,解决了3和4的问题 - 但我还没有测试过。