有条件加OutputCache指令 [英] Conditionally add OutputCache Directive

查看:145
本文介绍了有条件加OutputCache指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用我的aspx页面的OutputCache指令。我想补充,或根据环境中删除。例如,对生产的指导,而不是开发环境。

I'm using the OutputCache directive in my aspx pages. I would like to add it or remove it based on the environment. For example, have the directive for production, but not development environments.

这是如果在页面上声明似乎并没有工作:

An if statement on the page doesn't seem to work:

<% if (someCondition) { %>
  <%@ OutputCache Location="Any" Duration="1800" VaryByParam="None" %>
<% } %>

有没有一种方法,以消除(或增加),在后面的code指令?什么是实现这一目标的最佳方式?

Is there a way to remove (or add) a directive in the code behind? What's the best way to accomplish this?

推荐答案

每个属性的OutputCache可以编程的方式处理,如下所示:的如何使用Visual C在ASP.NET缓存#.NET

Every OutputCache attribute can be handled programmatically as shown here: How to cache in ASP.NET by using Visual C# .NET

有没有一种方法,以消除(或增加)在code指令后面?

不,我已经看到,到目前为止,但不排除可能性。尽管如此,你就不必为你的 Response.Cache 的在您的处置为您的方案,让你整十码一起玩。

Not that I have seen so far but not excluding the possibility. Nevertheless you would not have to as you have Response.Cache at your disposal for your scenario which gives you the whole ten yards to play with.

但如果你正在使用ASP.NET MVC,那么你可以使用它作为属性如下所示:
OutputCacheAttribute类([的OutputCache(CacheProfile =我的配置文件,持续时间= 10)])。
有一个计算器的例子,以及<一个href=\"http://stackoverflow.com/questions/1167890/how-to-programmatically-clear-outputcache-for-controller-action-method\">here.

Although if you are using ASP.NET MVC, then you can use it as attributes as shown here: OutputCacheAttribute Class ([OutputCache(CacheProfile = "MyProfile", Duration = 10)]). There is a stackoverflow example as well here.

什么是实现这一目标的最佳方式?

使用的 Response.Cache 的。检查低于code。

Using Response.Cache. Check the code below.

using System.Web;

//......
//......
//......

if (someCondition) 
{
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.Public); //Location="Any"
HttpContext.Current.Response.Cache.SetExpires(DateTime.Now.AddSeconds(1800)); //Duration="1800"
HttpContext.Current.Response.Cache.SetValidUntilExpires(true); 
}

这篇关于有条件加OutputCache指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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