简体   繁体   English

如何以编程方式在ASP.NET MVC中启动应用程序时禁用OutputCache

[英]How to programmatically disable OutputCache on application start in ASP.NET MVC

I would like the OutputCache feature to be disabled if the system.web.compilation setting in the web.config is set to debug="true" . 如果将web.config中的system.web.compilation设置设置为debug="true"我希望禁用OutputCache功能。

I can successfully access this value by calling this method in my Global.asax's Application_Start() : 我可以通过在Global.asax的Application_Start()调用此方法来成功访问此值:

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new HandleErrorAttribute());
    CompilationSection configSection = (CompilationSection)ConfigurationManager.GetSection("system.web/compilation");
    if (configSection?.Debug == true)
    {
        filters.Add(new OutputCacheAttribute()
        {
            VaryByParam = "*",
            Duration = 0,
            NoStore = true
        });
    }
}

The problem is, any endpoints in my controllers that have OutputCache explicitly set will not use the global filter that was set up. 问题是,控制器中显式设置了OutputCache所有端点都不会使用已设置的全局过滤器。

[OutputCache(CacheProfile = "Month")]
[HttpGet]
public ViewResult contact()
{
    return View();
}

Here is where that "Month" profile is defined in my web.config: 这是在我的web.config中定义“月份”配置文件的位置:

<system.web>
  <caching>
    <outputCacheSettings>
      <outputCacheProfiles>
        <add name="Month" duration="2592000" location="Any" varyByParam="*" />
      </outputCacheProfiles>
    </outputCacheSettings>
  </caching>
</system.web>

I need to be able to nullify the use of explicitly defined OutputCache profiles, like "Month", when I'm in debugging mode. 当我处于调试模式时,我需要能够取消对显式定义的OutputCache配置文件(如“月”)的使用。 How can I do this? 我怎样才能做到这一点?

You can create special web.config file for Debug only configuration, where you can declare profile like this: 您可以为仅调试配置创建特殊的web.config文件,您可以在其中声明配置文件,如下所示:

<system.web>
  <caching>
    <outputCacheSettings>
      <outputCacheProfiles>
        <add name="Month" duration="0" location="Any" varyByParam="*" />
      </outputCacheProfiles>
    </outputCacheSettings>
  </caching>
</system.web>

And when your application is built in Debug configuration you will have no cache. 而且,当您的应用程序以Debug配置构建时,您将没有缓存。

转换配置

不同的配置

Now you just need to implement transformation according to the article 现在,您只需要根据文章实施转换

@Back's answer is probably the most straight-forward solution that will work for the standard use case of wanting all caching disabled while debugging. @Back的答案可能是最简单的解决方案,适用于在调试时禁用所有缓存的标准用例。 However, because it is not a programmatic solution, I've come up with a way to do it all in code, which will also work for more advanced use cases. 但是,因为它不是编程解决方案,所以我想出了一种用代码完成所有操作的方法,该方法也适用于更高级的用例。

First, we want to capture whether or not the app is in debugging mode when it is launched. 首先,我们要捕获应用程序在启动时是否处于调试模式。 We'll store that in a global variable to keep things speedy. 我们会将其存储在全局变量中,以保持快速运行。

public static class GlobalVariables
{
    public static bool IsDebuggingEnabled = false;
}

Then in the Global.asax code's Application_Start method, write to the global property. 然后在Global.asax代码的Application_Start方法中,写入global属性。

protected void Application_Start()
{
    SetGlobalVariables();
}

private void SetGlobalVariables()
{
    CompilationSection configSection = (CompilationSection)ConfigurationManager
        .GetSection("system.web/compilation");
    if (configSection?.Debug == true)
    {
        GlobalVariables.IsDebuggingEnabled = true;
    }
}

Now we will create our own class to use for caching, which will inherit from OutputCacheAttribute . 现在,我们将创建自己的用于缓存的类,该类将从OutputCacheAttribute继承。

public class DynamicOutputCacheAttribute : OutputCacheAttribute
{
    public DynamicOutputCacheAttribute()
    {
        if (GlobalVariables.IsDebuggingEnabled)
        {
            this.VaryByParam = "*";
            this.Duration = 0;
            this.NoStore = true;
        }
    }
}

Now when you decorate your controller endpoints for caching, simply use your new attribute instead of [OutputCache] . 现在,当您装饰控制器端点进行缓存时,只需使用new属性而不是[OutputCache]

// you can use CacheProfiles or manually pass in the arguments, it doesn't matter.
// either way, no caching will take place if the app was launched with debugging
[DynamicOutputCache(CacheProfile = "Month")]
public ViewResult contact()
{
    return View();
}

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

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