mvc3简单的自定义输出缓存

时间:2012-10-30 08:49:41

标签: asp.net-mvc-3 caching

我正在尝试按照一些教程在一个地方我应该覆盖GetVaryByCustomString .... 代码如下

public override string GetVaryByCustomString(HttpContext context, string arg)
        {
            // It seems this executes multiple times and early, so we need to extract language again from cookie.
            if (arg == "culture") // culture name (e.g. "en-US") is what should vary caching
            {
                string cultureName = null;
                // Attempt to read the culture cookie from Request
                HttpCookie cultureCookie = Request.Cookies["_culture"];
                if (cultureCookie != null)
                    cultureName = cultureCookie.Value;
                else
                    cultureName = Request.UserLanguages[0]; // obtain it from HTTP header AcceptLanguages

                // Validate culture name
                cultureName = CultureHelper.GetImplementedCulture(cultureName); // This is safe

                return cultureName.ToLower();// use culture name as cache key, "es", "en-us", "es-cl", etc.
            }

            return base.GetVaryByCustomString(context, arg);
        }

问题是:我应该把这些代码放在哪个类中?

1 个答案:

答案 0 :(得分:0)

您可以在 Global.asax

中覆盖它