输出缓存是在 ASP.NET MVC2 中使用下面的代码实现的。
GetVaryByCustomString
方法未调用:将断点置于其第一行并运行应用程序显示未到达断点。到达控制器 Index() 中的断点。
如何VaryByCustom
在 ASP.NET MVC2 中使用?
控制器:
[OutputCache(VaryByCustom = "user")]
public ActionResult Index(string _entity, string id)
{
...
全球.asax.cs:
public class MvcApplication : System.Web.HttpApplication
{
public override string GetVaryByCustomString(HttpContext context, string arg)
{
if (arg == "user")
{
HttpCookie cookie = context.Request.Cookies["Company"];
if (cookie != null)
return Thread.CurrentPrincipal.Identity.Name + "," + cookie.Value;
return Thread.CurrentPrincipal.Identity.Name;
}
return base.GetVaryByCustomString(context, arg);
}
}