【发布时间】:2012-10-17 09:47:49
【问题描述】:
使用下面的代码在 ASP.NET MVC2 中实现输出缓存。
GetVaryByCustomString 方法未调用:将断点置于其第一行并运行应用程序显示未到达断点。
到达控制器 Index() 中的断点。
如何在ASP.NET MVC2中使用VaryByCustom?
控制器:
[OutputCache(VaryByCustom = "user")]
public ActionResult Index(string _entity, string id)
{
...
Global.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);
}
}
【问题讨论】:
标签: c# asp.net-mvc asp.net-mvc-2 outputcache