开发者

OutputCache controller attribute vary by user role? Is this possible in .net MVC?

开发者 https://www.devze.com 2022-12-20 18:55 出处:网络
Is it possible to output cache controller actions differently based on user role? or if they are authenti开发者_开发技巧cated or not?Take a look at VaryByCustom.

Is it possible to output cache controller actions differently based on user role? or if they are authenti开发者_开发技巧cated or not?


Take a look at VaryByCustom.

http://msdn.microsoft.com/en-us/library/system.web.httpapplication.getvarybycustomstring.aspx


[Careful: This answer was valid as of 2011]

We add the OutputCache directive like this:

<%@ OutputCache Duration="60" VaryByParam="None" VaryByCustom="SessionID" %>

In MVC, add this attribute to your action

[OutputCache(Duration = 60, VaryByParam="None", VaryByCustom="SessionID")]

Then, in the Global.asax file

Public override string GetVaryByCustomString(HttpContext context, string arg) 
{ 
  if(arg.ToLower() == "sessionid") 
  { 
    HttpCookie cookie = context.Request.Cookies["ASP.NET_SessionID"]; 
    if(cookie != null) 
      return cookie.Value; 
  } 
  return base.GetVaryByCustomString(context, arg); 
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号