And*_*rus 8 c# asp.net-mvc outputcache asp.net-mvc-2
使用下面的代码在ASP.NET MVC2中实现输出缓存.
GetVaryByCustomString不调用方法:将断点放在第一行,运行应用程序显示未到达断点.达到控制器Index()中的断点.
如何VaryByCustom 在ASP.NET MVC2中使用?
控制器:
[OutputCache(VaryByCustom = "user")]
public ActionResult Index(string _entity, string id)
{
...
Run Code Online (Sandbox Code Playgroud)
的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);
}
}
Run Code Online (Sandbox Code Playgroud)
您的OutputCache定义是错误的.您必须指定Duration:
[OutputCache(VaryByCustom = "user", Duration = 50)]
public ActionResult Index(string _entity, string id)
Run Code Online (Sandbox Code Playgroud)
现在GetVaryByCustomString将调用您重写的方法.另外不要忘记GetVaryByCustomString只有在控制器操作完成执行后才会调用该方法.
| 归档时间: |
|
| 查看次数: |
3718 次 |
| 最近记录: |